2010-06-30 5 views
3

Windows 모바일 응용 프로그램에서 작업 중이며 Google지도로 현재 위치를 표시하고 싶습니다. 샘플에서 Location dll을 사용했습니다. 아래 코드에서 알 수 있듯이 gps_Locationchanged 이벤트에서지도를 업데이트하는 적절한 방법을 호출합니다. 여기서는 Invoke 메서드를 사용하여 pictureboxe의 이미지를 업데이트합니다. 문제는 응용 프로그램의 메인 메뉴와 컨텍스트 메뉴를 언제든지 사용할 수 없다는 것입니다. 마치 새로운지도 다운로드가 끝날 때까지 고정되는 것과 같습니다. 다른 스레드에서이 작업을 수행하는 다른 방법이 있으므로 언제든지 사용할 수 있습니까?C# show gps location on map

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

3

그것은 확실히 말할 어렵다,하지만 문제가 서버에서 실제 이미지를 얻을 수 (나는 가정)을 image_request2() 메소드처럼 보인다. 작업자 스레드에서이 메서드를 실행하고 완전히 다운로드되면 화면에 이미지를 그리는 간단한 콜백 메서드를 제공하면 사용자의 이벤트를 받기 위해 UI 스레드가 열려있게됩니다.