如何从另一个线程引发事件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服务等待无休止的队列终止。
答案 0 :(得分:4)
看看
http://www.codeproject.com/KB/cs/Cross_thread_Events.aspx
另请参见BackgroundWorker类:http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx
答案 1 :(得分:3)
查看Invoke()
方法: