views:

56

answers:

2

I have a GPS class which i obviously use to acquire the latitudes and longitudes.

The UpdateData() is called when the gps state or location is changed. So i make sure that both the Latitude and Longitude is valid and then i make the DataReady bool equal to true.

So i use this code in a fairly n00b fashion where i use a Timer (Windows Form's one) and check getGPSCoordinates() every 10 seconds to see whether a valid location is found (getGPSCoordinates() is not null) and if not, do the rest of code. This is used, as it may take from 10 seconds to maybe even 1 minute to get a clear gps signal.

So although this works fine, i know this is not the correct way to do this. I think whole GPS business should be carried on a separate thread and it should notify the main thread that the location is changed and the data is ready.

Can someone point me on the correct way of handling these kinds of business? Should i use IAsyncResult to notify the main thread?

bool DataReady = false;

private void UpdateData()
        {

            if (gps.Opened)
            {
                if (position != null)
                {

                    if (position.LatitudeValid)
                    {
                        Latitude =  position.Latitude.ToString();
                    }


                    if (position.LongitudeValid)
                    {
                        Longitude = position.Longitude.ToString();

                    }

                    if (Latitude != null && Longitude != null)
                        DataReady = true;          


                }



            }


 public string getGPSCoordinates()
        {
            if (DataReady)
            {
                return String.Format("http://maps.google.com/maps/api/staticmap?sensor=false&size=500x500&markers=color:red|label:A|{0},{1}&zoom=15&maptype=hybrid", Latitude, Longitude);
            }
            else
                return null;
        }


        }
+1  A: 

It sounds like you want to use a BackgroundWorker - however this isn't natively supported in the Compact Framework. Again however, this can be addressed:

http://stackoverflow.com/questions/1323596/net-compact-fw-3-5-background-worker

You can then call into your background worker whenever your UI timer ticks (make sure to pause your UI timer until you get a response).

If you cannot use the implemented BackgroundWorker from that question, you will need to maintain a Queue or List that both the UI and the background can see. Using a normal thread will likely mean registering a callback (fairly standard, using your IAsyncResult idea) however this callback is run on an unknown, arbitrary thread - this makes updating your UI a little finicky.

Adam
I don't need to update the UI. I just need to get the GPS result to a string so i can send an SMS :)
Ranhiru Cooray
Then a callback will be fine, but subsequently if it's not visible to the user then stick with your timer implementation.
Adam
+2  A: 

Unless you are planning to use your app on the Space Shuttle, I cannot come up with a good reason to burn a resource as expensive as a thread on this task. A Windows Forms Timer is fine. Do reduce the Interval to, say, a second as long as you haven't received a good measurement so the user won't have to wait so long. Once you do, increase the Interval. Although I doubt it makes much difference.

Hans Passant