C#、事件处理程序和线程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1067532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C#, Event Handlers and Threading
提问by anonymous coward
I'm writing a little chat app, and I have this event handler:
我正在编写一个小聊天应用程序,我有这个事件处理程序:
void o_Typing(object sender, EventArgs e)
{
MessageBox.Show("Fired!");
this.Text = "Fired!";
}
o_Typing
is a method in a class derived from TabPage
. Basically, I want each conversation to have it's own tab.
o_Typing
是派生自 的类中的方法TabPage
。基本上,我希望每个对话都有自己的标签。
The event handlers are fired by my Chat object, which is running in another thread. I have 1 thread for UI, and another thread for each Chat conversation (to keep polling the server for new data)
事件处理程序由我的 Chat 对象触发,该对象在另一个线程中运行。我有 1 个 UI 线程,每个聊天对话都有另一个线程(以不断轮询服务器以获取新数据)
When the event is fired, the MessageBox
pops up, but the Tab caption doesn't change. After the event has fired once, it never fires again, leading me to believe that the event is being called in the worker thread, although it is defined in the UI thread.
当事件被触发时,会MessageBox
弹出,但 Tab 标题不会改变。事件触发一次后,它再也不会触发,这让我相信该事件是在工作线程中调用的,尽管它是在 UI 线程中定义的。
How can I get my events to be called from the worker thread, and use Invoke()
to get them to execute on the UI thread?
如何让我的事件从工作线程调用,并用于Invoke()
让它们在 UI 线程上执行?
采纳答案by Jon Skeet
There are two options:
有两种选择:
1) Make the event handlers thread-safe: use Control.Invoke/BeginInvoke
in any event handler which needs to talk to the UI thread.
1) 使事件处理程序线程安全:Control.Invoke/BeginInvoke
在需要与 UI 线程通信的任何事件处理程序中使用。
2) Make the worker thread marshal back to the UI thread before raising the event - in other words, use Control.Invoke
as part of the process of raising the event, so that the event handlers will all be called in the UI thread. Depending on how your app is structured, you may not want your event-raising component to know about the UI explicitly - but when it's being constructed you can pass in an ISynchronizeInvoke
(which Control
implements) and your component can use that to raise its events on the right thread. Of course, that only works (simply, anyway) if every event handler is happy to run on the same thread - but that will often be the case. You'd write something like:
2) 使工作线程在引发事件之前封送回 UI 线程 - 换句话说,Control.Invoke
用作引发事件过程的一部分,以便在 UI 线程中调用所有事件处理程序。根据您的应用程序的结构,您可能不希望您的事件引发组件明确了解 UI - 但是在构建它时,您可以传入一个ISynchronizeInvoke
(它Control
实现)并且您的组件可以使用它来引发其事件正确的线程。当然,这只有在每个事件处理程序都乐于在同一线程上运行时才有效(简单地说,无论如何) - 但情况通常如此。你会写这样的:
protected void OnFoo(EventArgs args)
{
if (sync != null && sync.InvokeRequired)
{
sync.Invoke((Action) delegate { OnFoo(args) }, null);
return;
}
EventHandler handler = Foo; // Where Foo is the event name
if (handler != null)
{
handler (this, args);
}
}
回答by Oliver Hanappi
If you fire your event in code which is executed by your worker thread, then all methods subscribed to the event will be executed under that worker thread.
如果您在工作线程执行的代码中触发事件,那么订阅该事件的所有方法都将在该工作线程下执行。
For GUI-elements you need to look at the Invoke-methods.
对于 GUI 元素,您需要查看调用方法。
Best Regards
此致