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.
更多推荐
发布评论