【发布时间】:2017-05-17 14:35:56
【问题描述】:
我正在编写一个触发事件的库。此库启动第二个线程,该线程连接到服务器并侦听消息(阻塞调用,第二个线程的原因)。
public virtual event LogEventHandler EntryReceived;
protected virtual void ReceiveEntry(ILogEntry entry)
{
if (EntryReceived != null)
EntryReceived(this, new LogEventArgs() { Entry = entry });
}
当从服务器接收到消息时,它会触发一个事件:
ReceiveEntry(entry);
我希望最终开发人员不必在他的事件处理程序中考虑 InvokeRequired/Invoke sn-p。我如何确保在“父”线程(我知道它与实例化我的类的线程相同)上触发我的事件?
【问题讨论】:
标签: c# multithreading events invoke