2010-06-30 215 views
3

我在一个Windows Mobile应用程序中工作,我想用谷歌地图显示我当前的位置。我使用了示例中的Location dll。正如你在我的代码中看到的,我调用了更新地图的方法,我使用Invoke方法来更新pictureboxe的图像。问题是我无法使用应用程序的主菜单和上下文菜单。这就像他们冻结,直到新地图完成下载。是否有另一种方式在不同的线程中做到这一点,以便随时使用它们?C#在地图上显示gps位置

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

也许沿着这些路线的东西?

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; 
     } 
    } 
+0

非常感谢你ctacke它工作:) – stefos 2010-06-30 17:17:40

3

很难肯定地说,但它看起来像(我认为)从服务器获取实际图像的问题image_request2()方法。如果要在工作线程上运行此方法,并提供一个简单的回调方法,可以在完全下载后在屏幕上绘制图像,这会让UI线程处于打开状态以接收来自用户的事件。