views:

57

answers:

2

I am working in a windows mobile application and I want to show my current location with google maps. I used the Location dll from the samples. As you see below in my code, I call the proper method for updating the map in the gps_Locationchanged event where I use the Invoke method to update the pictureboxe's image. The problem is that I can't use the main menu and the context menu of the application whenever i want. It's like they freeze until the new map finish downloading. Is there another way to do that in different thread so they can be used anytime?

void gps_LocationChanged(object sender, LocationChangedEventArgs args)
{
    if (args.Position.LatitudeValid && args.Position.LongitudeValid)
    {

       pictureBox1.Invoke((UpdateMap)delegate()
         {
             center.Latitude = args.Position.Latitude;
             center.Longitude = args.Position.Longitude;
             LatLongToPixel(center);
             image_request2(args.Position.Latitude, args.Position.Longitude);

         });
    }
}
+3  A: 

It's hard to say for sure, but it looks like the image_request2() method that (I assume) gets the actual image from the server is the problem. If you were to run this method on a worker thread, and provide a simple callback method that would paint the image on the screen once it's completely downloaded, this would leave your UI thread open to receive events from the user.

highphilosopher
+2  A: 

Maybe something along these lines?

    bool m_fetching;

    void gps_LocationChanged(object sender, LocationChangedEventArgs args)
    {
        if (m_fetching) return;

        if (args.Position.LatitudeValid && args.Position.LongitudeValid)
        {
            ThreadPool.QueueUserWorkItem(UpdateProc, args);
        }
    }

    private void UpdateProc(object state)
    {
        m_fetching = true;

        LocationChangedEventArgs args = (LocationChangedEventArgs)state;
        try
        {
            // do this async
            var image = image_request2(args.Position.Latitude, args.Position.Longitude);

            // now that we have the image, do a synchronous call in the UI
            pictureBox1.Invoke((UpdateMap)delegate()
            {
                center.Latitude = args.Position.Latitude;
                center.Longitude = args.Position.Longitude;
                LatLongToPixel(center);
                image;
            });
        }
        finally
        {
            m_fetching = false;
        }
    }
ctacke
Thank you very much ctacke it worked :)
stefos