gpt4 book ai didi

c# - 多线程时将参数传递给方法

转载 作者:太空宇宙 更新时间:2023-11-03 20:24:25 24 4
gpt4 key购买 nike

因此,我正在尝试将一个参数传递给一个我想参与多线程的方法。所以,我编写了如下代码:

    new Thread (Go(ineedthis)).Start();
Go();

static void Go(string ineedthis)
{
lock (locker)
{
if (!done) { Console.WriteLine ("Done"); done = true; }
}
}

但是,我不能传递参数ineedthis,因为当你像我在第一行中那样插入它时会报错。反之,如果在为方法创建线程时不给参数,也会报错。

那么,如何在创建线程时将参数传递给方法呢?

谢谢!注意:我昨天才开始使用 c#,所以我对此完全陌生。请详细解释,以便我从中得到更多!

编辑 - 错误:

Error   1   The best overloaded method match for 'System.Threading.Thread.Thread(System.Threading.ParameterizedThreadStart)' has some invalid arguments     23  21  test


Error 2 Argument 1: cannot convert from 'method group' to 'System.Threading.ParameterizedThreadStart' 23 32 test

最佳答案

我认为您正在寻找更像这样的东西:

  var t = new Thread (Go); 
t.Start(ineedthis);

您首先创建一个线程,详细说明在后台线程上运行时该方法的内容。然后启动线程,根据需要传入任何参数。参见 MSDN了解更多信息。

关于c# - 多线程时将参数传递给方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11420158/

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