Using C# MethodInvoker.Invoke() for a GUI app… is this good?

前端 未结 5 1786
死守一世寂寞
死守一世寂寞 2020-12-01 01:01

Using C# 2.0 and the MethodInvoker delegate, I have a GUI application receiving some event from either the GUI thread or from a worker thread.

I use the following pa

5条回答
  •  盖世英雄少女心
    2020-12-01 01:40

    Personally I like this method:

    private void ExecuteSecure(Action a)
    {
        if (InvokeRequired)
            BeginInvoke(a);
        else
            a();
    }
    

    And then you can write one-liners like this:

    ExecuteSecure(() => this.Enabled = true);
    

提交回复
热议问题