作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
如何从另一个线程引发事件 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;
}
实际上,线程在委托(delegate)范围终止后终止。这可能是一种愚蠢的方法,但我在该范围内放置了一段时间 Queue.empty { sleep } 所以它永远不会终止,然后我从另一个线程启动 LoadMap ,这样它会阻塞我的 WCF 服务等待永无休止的队列终止。
最佳答案
看看
http://www.codeproject.com/KB/cs/Cross_thread_Events.aspx
另见 BackgroundWorker 类:http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx
关于c# - 如何引发跨线程事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8433259/
我是一名优秀的程序员,十分优秀!