C#,从后台线程调用匿名方法(Action )(C#, invoke anonymous method (Action) from background thread)

编程入门 行业动态 更新时间:2024-10-27 22:32:33
C#,从后台线程调用匿名方法(Action <>)(C#, invoke anonymous method (Action<>) from background thread)

这应该很简单!

我想创建一个匿名的Action <>委托来执行GUI更新,我将从其他几个匿名委托调用它(它将在不同的线程上运行)。

void Test() { Action<string> invokeDisplay = new Action<string>(delegate(string Element) { //Do a variety of things to my GUI depending on Element parameter }); MethodInvoker opLong1 = new MethodInvoker(delegate() { // Do long task this.Invoke(invokeDisplay("long1")); }); MethodInvoker opLong2 = new MethodInvoker(delegate() { // Do long task this.Invoke(invokeDisplay("long2")); }); new Thread(new ThreadStart(opLong1)).Start(); new Thread(new ThreadStart(opLong2)).Start(); }

那么这条线的正确语法是什么?

this.Invoke(invokeDisplay("long1"));

This should be simple!

I want to create an anonymous Action<> delegate to perform a GUI update, which I will call from several other anonymous delegates (which will be run on separate threads).

void Test() { Action<string> invokeDisplay = new Action<string>(delegate(string Element) { //Do a variety of things to my GUI depending on Element parameter }); MethodInvoker opLong1 = new MethodInvoker(delegate() { // Do long task this.Invoke(invokeDisplay("long1")); }); MethodInvoker opLong2 = new MethodInvoker(delegate() { // Do long task this.Invoke(invokeDisplay("long2")); }); new Thread(new ThreadStart(opLong1)).Start(); new Thread(new ThreadStart(opLong2)).Start(); }

So whats the correct syntax for this line?

this.Invoke(invokeDisplay("long1"));

最满意答案

语法是:

Invoke(action, "long1");

委托是第一个参数,您要传递给它的参数如下。

The syntax would be:

Invoke(action, "long1");

The delegate is the first parameter, and the argument(s) you want to pass to it follow.

更多推荐

本文发布于:2023-04-28 07:48:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1331379.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:线程   后台   方法   Action   invoke

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!