2011-12-08 76 views
4

如何从另一个线程中引发事件GeocodeAddressEventHandler?如何提高交叉线程事件

System.Threading.Thread MapThread; 
    WinformMap map ; 

    public delegate void GeocodeAddressEventHandler(object sender, EventArgs e); 
    public event GeocodeAddressEventHandler GeocodeAddressEvent; 

    //How to Raise this Event from another thread?? 
    public void GeocodeAddress_Raised(object sender, EventArgs e) 
    { 
     MapLib.GeocodeAddress("12798 1ST ST", "", "", ""); 
    } 

    public bool LoadMap(string restorepoint) 
    { 
     ////////////////////////Engine Controls//////////////////////////////////////////////////////// 
     try 
     { 
      System.ServiceModel.OperationContext context = System.ServiceModel.OperationContext.Current; 

      //This is to instantiate a winform from a Console (will convert to service shortly) 
      MapThread = new System.Threading.Thread(new System.Threading.ThreadStart(delegate 
      { 
       using (System.ServiceModel.OperationContextScope scope = new System.ServiceModel.OperationContextScope(context)) 
       { 
this.GeocodeAddressEvent += new GeocodeAddressEventHandler(GeocodeAddress_Raised); 
       } 
      })); 
      MapThread.SetApartmentState(System.Threading.ApartmentState.STA); 
      MapThread.Start(); 
      MapThread.Join(); 
     } 
     catch (Exception ex) 
     { 
      return false; 
     } 

     return true; 
    } 

实际上,事实证明,线程在委托范围终止后终止。这可能是一种愚蠢的做法,但是我在该范围内放置了一段Queue.empty {sleep},因此它永远不会终止,然后我从另一个线程启动LoadMap,以便阻塞我的WCF服务,等待无休止的队列终止。

回答