【发布时间】:2010-12-01 18:11:30
【问题描述】:
我有一个 C# 2.0 应用程序,其表单使用包含线程的类。
在线程函数中,不是直接调用事件处理程序,而是调用它。效果是拥有的表单不需要调用 InvokeRequired/BeginInvoke 来更新其控件。
public class Foo
{
private Control owner_;
Thread thread_;
public event EventHandler<EventArgs> FooEvent;
public Foo(Control owner)
{
owner_ = owner;
thread_ = new Thread(FooThread);
thread_.Start();
}
private void FooThread()
{
Thread.Sleep(1000);
for (;;)
{
// Invoke performed in the thread
owner_.Invoke((EventHandler<EventArgs>)InternalFooEvent,
new object[] { this, new EventArgs() });
Thread.Sleep(10);
}
}
private void InternalFooEvent(object sender, EventArgs e)
{
EventHandler<EventArgs> evt = FooEvent;
if (evt != null)
evt(sender, e);
}
}
public partial class Form1 : Form
{
private Foo foo_;
public Form1()
{
InitializeComponent();
foo_ = new Foo(this);
foo_.FooEvent += OnFooEvent;
}
private void OnFooEvent(object sender, EventArgs e)
{
// does not need to call InvokeRequired/BeginInvoke()
label_.Text = "hello";
}
}
这显然与使用 System.Timers.Timer 和 System.Io.Ports.SerialPort 等后台线程的 Microsoft API 使用的方法相反。这种方法有什么本质上的错误吗?有什么危险吗?
谢谢, 保罗H
编辑:另外,如果表单没有立即订阅事件怎么办?表单不感兴趣的事件是否会阻塞表单的消息队列?
【问题讨论】:
标签: c# .net multithreading events invoke