Shortest way to write a thread-safe access method to a windows forms control

后端 未结 5 1029
北海茫月
北海茫月 2020-12-02 11:47

In this article:

http://msdn.microsoft.com/en-us/library/ms171728(VS.80).aspx

The author uses the following method to make thread-safe calls to a Windows For

5条回答
  •  萌比男神i
    2020-12-02 12:20

    1) Using anonymous delegate

    private void SetText(string text)
    {
        if (this.InvokeRequired)
        {    
            Invoke(new MethodInvoker(delegate() {
                SetText(text);
            }));
        }
        else
        {
            this.textBox1.Text = text;
        }
    }
    

    2) AOP approach

    [RunInUIThread]
    private void SetText(string text)
    {
        this.textBox1.Text = text;
    }
    

    http://weblogs.asp.net/rosherove/archive/2007/05.aspx?PageIndex=2

    3) Using lambda expressions (outlined by others).

提交回复
热议问题