D
Daniel Root
I am writing a multi-threaded application using the CF and have a basic
threading question:
I read in the CF Core reference that it's not a good idea to update
instance members of a class from a threaded proccess, but am unclear how
this applies, and how to safely update instance members without the
consuming class having to "know" about mutexes and the like...
For example, is the following pseudocode "bad":
class GPSManager
{
private float _Lat;
private float _Long;
public float Lat; //property to expose _Lat. get code left off..
public float Long;
private PollGPSCallback(object state)
{
//read GPS...
_Lat = gpsreader.GetLat();
_Long = gpsreader.GetLong();
//raise event
}
private tmr;
public Start()
{
// create callback delegate...
tmr=new System.Threading.Timer(cbPollGPS,null,0,5000);
}
public Stop()
{//stop timer, clear resources...}
....
}
The idea being that the consuming class can create an instance of
GPSManager, call Start(), and access the properties as needed...
In my testing, I haven't run into any issues accessing the properties
(Lat and Long) from the main thread, but am a bit nervous I could be
missing something. Is there something I should be doing to lock the code
within GPSCallback and/or in the properties' "get" code?
Thanks in advance!
Daniel Root
threading question:
I read in the CF Core reference that it's not a good idea to update
instance members of a class from a threaded proccess, but am unclear how
this applies, and how to safely update instance members without the
consuming class having to "know" about mutexes and the like...
For example, is the following pseudocode "bad":
class GPSManager
{
private float _Lat;
private float _Long;
public float Lat; //property to expose _Lat. get code left off..
public float Long;
private PollGPSCallback(object state)
{
//read GPS...
_Lat = gpsreader.GetLat();
_Long = gpsreader.GetLong();
//raise event
}
private tmr;
public Start()
{
// create callback delegate...
tmr=new System.Threading.Timer(cbPollGPS,null,0,5000);
}
public Stop()
{//stop timer, clear resources...}
....
}
The idea being that the consuming class can create an instance of
GPSManager, call Start(), and access the properties as needed...
In my testing, I haven't run into any issues accessing the properties
(Lat and Long) from the main thread, but am a bit nervous I could be
missing something. Is there something I should be doing to lock the code
within GPSCallback and/or in the properties' "get" code?
Thanks in advance!
Daniel Root