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

时间:2022-10-08 13:51:14

I'm using this code with .NET 3.0

我在。net 3.0中使用了这段代码

Action xx = () => button1.Text = "hello world";
this.Invoke(xx);

but when i tried it in .NET 2.0, I think Action has type parameter like this:

但是当我在。net 2.0中尝试时,我认为Action有如下类型参数:

Action<T>

How to implement the first code in .NET 2.0?

如何在。net 2.0中实现第一个代码?

2 个解决方案

#1


6  

Try this:

试试这个:

this.Invoke((MethodInvoker) delegate
{
    button1.Text = "hello world";
});

Although Action was introduced in .NET 2.0, you cannot use lambda expression () => ... syntax in .NET 2.0.

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

BTW, you can still use Action in .NET 2.0, as long as you don't use lambda sytax:

顺便说一句,你仍然可以在。net 2.0中使用Action,只要你不使用lambda sytax:

Action action = delegate { button1.Text = "hello world"; };
Invoke(action);

#2


1  

Action<T> is the signature, which just means that the method represented by the action must take a single parameter. What the type of the parameter is, depends on the signature of the Invoke call.

动作 是签名,这意味着动作表示的方法必须采用单个参数。参数的类型取决于调用的签名。

Some code examples of how to represent the various signatures of Action:

如何表示各种签名的一些代码示例:

var noArgs = () => button1.Text = "hello world"; // Action
var oneArg = (arg) => button1.Text = "hello world"; // Action<T>
var twoArgs = (arg1, arg2) => button1.Text = "hello world"; // Action<T,T>

If you don't need to use the parameters to the method, that's fine. But you still need to declare them in the lambda expression.

如果您不需要使用方法的参数,那也没关系。但是你仍然需要在lambda表达式中声明它们。

Now, this doesn't answer how to do it from .NET 2.0, but I assumed (perhaps incorrectly, correct me if I'm wrong) that you weren't aware how lambdas correspond to Action types.

现在,这并没有回答如何从。net 2.0实现它,但是我假设(如果我错了,请纠正我)您不知道lambdas是如何与动作类型对应的。

#1


6  

Try this:

试试这个:

this.Invoke((MethodInvoker) delegate
{
    button1.Text = "hello world";
});

Although Action was introduced in .NET 2.0, you cannot use lambda expression () => ... syntax in .NET 2.0.

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

BTW, you can still use Action in .NET 2.0, as long as you don't use lambda sytax:

顺便说一句,你仍然可以在。net 2.0中使用Action,只要你不使用lambda sytax:

Action action = delegate { button1.Text = "hello world"; };
Invoke(action);

#2


1  

Action<T> is the signature, which just means that the method represented by the action must take a single parameter. What the type of the parameter is, depends on the signature of the Invoke call.

动作 是签名,这意味着动作表示的方法必须采用单个参数。参数的类型取决于调用的签名。

Some code examples of how to represent the various signatures of Action:

如何表示各种签名的一些代码示例:

var noArgs = () => button1.Text = "hello world"; // Action
var oneArg = (arg) => button1.Text = "hello world"; // Action<T>
var twoArgs = (arg1, arg2) => button1.Text = "hello world"; // Action<T,T>

If you don't need to use the parameters to the method, that's fine. But you still need to declare them in the lambda expression.

如果您不需要使用方法的参数,那也没关系。但是你仍然需要在lambda表达式中声明它们。

Now, this doesn't answer how to do it from .NET 2.0, but I assumed (perhaps incorrectly, correct me if I'm wrong) that you weren't aware how lambdas correspond to Action types.

现在,这并没有回答如何从。net 2.0实现它,但是我假设(如果我错了,请纠正我)您不知道lambdas是如何与动作类型对应的。