gpt4 book ai didi

c# - 如何引发跨线程事件

转载 作者:太空狗 更新时间:2023-10-30 00:44:13 24 4
gpt4 key购买 nike

如何从另一个线程引发事件 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 服务等待永无休止的队列终止。

最佳答案

关于c# - 如何引发跨线程事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8433259/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com