【发布时间】:2016-10-12 16:24:15
【问题描述】:
我正在编写的类使用多线程,并且我为我的类创建了这个扩展方法,以便以线程安全的方式引发事件。
static class MultiThreadHelper
{
public static object Raise(this MulticastDelegate eventToRaise, object sender, EventArgs e)
{
object retVal = null;
MulticastDelegate threadSafeMulticastDelegate = eventToRaise;
if (threadSafeMulticastDelegate != null)
{
foreach (Delegate d in threadSafeMulticastDelegate.GetInvocationList())
{
var synchronizeInvoke = d.Target as ISynchronizeInvoke;
if ((synchronizeInvoke != null) && synchronizeInvoke.InvokeRequired)
{
retVal = synchronizeInvoke.EndInvoke(synchronizeInvoke.BeginInvoke(d, new[] { sender, e }));
}
else
{
retVal = d.DynamicInvoke(new[] { sender, e });
}
}
}
//Return the value of the event invocation or null if none.
return retVal;
}
}
当我的表单试图关闭一个挥之不去的线程时,它仍在尝试返回并引发一个不再有处理程序的事件。
我最终得到以下错误...
在该行错误发生之前我可以运行哪种检查?还有什么我可以做的或者我可以采取不同的方法来解决这个问题吗?
【问题讨论】:
标签: c# multithreading events .net-4.0 thread-safety