从线程调用Invoke / BeginInvoke

时间:2010-12-01 18:11:30

标签: c# .net multithreading events invoke

我有一个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使用的方法相反。这种方法有什么固有的错误吗?它在某种程度上是危险的吗?

谢谢,  PaulH


编辑:同样,如果表单没有立即订阅该活动该怎么办?是否会阻止Form的消息队列中包含表单不感兴趣的事件?

1 个答案:

答案 0 :(得分:3)

这是一个线程安全调用,该方法将在表单的线程中处理。

从概念角度来看它没有错。

但是,计时器对于此类任务更为优雅。但是,间隔为10ms的计时器可能会降低GUI的速度,这可能就是使用Invoke的原因。

您不需要调用InvokeRequired,因为很明显Control在另一个线程中。此外,只需要在异步调用方法时调用BeginInvoke,这显然不是这种情况。

关于你的编辑: 不,消息队列不会被阻塞。如果没有注册处理程序,则不会触发任何事件。再看看你的代码;)

相关问题