gpt4 book ai didi

c# - 如何创建线程?

转载 作者:IT王子 更新时间:2023-10-29 03:50:26 24 4
gpt4 key购买 nike

下面的方法是我想在该线程中完成的:

public void Startup(int port,string path)
{
Run(path);
CRCCheck2();
CRCCheck1();
InitializeCodeCave((ushort)port);
}

我尝试了我能找到的谷歌搜索,但没有任何效果

public void Test(int port,string path)
{
Thread t = new Thread(Startup(port,path));
}

public void TestA(int port,string path)
{
Thread t = new Thread(Startup);
t.Start (port,path);
}

两个都不编译,怎么办?

最佳答案

以下方法有效。

// The old way of using ParameterizedThreadStart. This requires a
// method which takes ONE object as the parameter so you need to
// encapsulate the parameters inside one object.
Thread t = new Thread(new ParameterizedThreadStart(StartupA));
t.Start(new MyThreadParams(path, port));

// You can also use an anonymous delegate to do this.
Thread t2 = new Thread(delegate()
{
StartupB(port, path);
});
t2.Start();

// Or lambda expressions if you are using C# 3.0
Thread t3 = new Thread(() => StartupB(port, path));
t3.Start();

这些示例的启动方法具有以下签名。

public void StartupA(object parameters);

public void StartupB(int port, string path);

关于c# - 如何创建线程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/811224/

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