从C#2.0中的另一个线程更新控件

Vin*_*pin 5 .net c# parallel-processing multithreading winforms

我在.NET 3.0中使用此代码

Action xx = () => button1.Text = "hello world";
this.Invoke(xx);
Run Code Online (Sandbox Code Playgroud)

但是当我在.NET 2.0中尝试它时,我认为Action有这样的类型参数:

Action<T>
Run Code Online (Sandbox Code Playgroud)

如何在.NET 2.0中实现第一个代码?

Ale*_*Aza 6

试试这个:

this.Invoke((MethodInvoker) delegate
{
    button1.Text = "hello world";
});
Run Code Online (Sandbox Code Playgroud)

虽然Action是在.NET 2.0中引入的,但您不能在.NET 2.0中使用lambda表达式() => ...语法.

顺便说一下Action,只要你不使用lambda sytax ,你仍然可以在.NET 2.0中使用:

Action action = delegate { button1.Text = "hello world"; };
Invoke(action);
Run Code Online (Sandbox Code Playgroud)