要在正确的线程上访问WindowsForms控件,我可以使用Control本身的Invoke方法

时间:2016-09-01 19:09:23

标签: c# multithreading winforms

我正在处理一个控件队列以响应Timer Elapsed事件。 我在MSDN上看到的所有教程都显示了这样的内容;

    string text = "abc";
    if (this.myControl.InvokeRequired)
    {
        // we are in a class that inherits from 'Form'
        this.Invoke(new MyDelegate(MyMethod), text);
    }
    else
    {
        MyMethod(text);
    }

我不是一个System.Windows.Form的类,所以我无法调用

  

this.Invoke(.....

这就是我没有表格来调用Invoke的方法;

    Control myControl = myQueue.Dequeue();
    if (myControl.InvokeRequired)
    {
        // Note that MyMethod takes myControl as an argument
        // and I am also using the Invoke method on myControl
        myControl.Invoke(new MyDelegate(MyMethod), myControl);
    }
    else
    {
        MyMethod(myControl);
    }

关于我是否应该在控件或其表格上调用Invoke,是否有任何规则/指南?使用其中任何一个有什么区别?

还有什么需要考虑的因为在委托中我正在访问我调用其Invoke方法的同一个Control吗?

0 个答案:

没有答案