gpt4 book ai didi

C#,从后台线程调用匿名方法(Action<>)

转载 作者:行者123 更新时间:2023-12-03 12:49:17 24 4
gpt4 key购买 nike

这应该很简单!

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

    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"));

最佳答案

语法是:

Invoke(action, "long1");

委托(delegate)是第一个参数,后面是您要传递给它的参数。

关于C#,从后台线程调用匿名方法(Action<>),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18621565/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com