首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

如何将参数传递给Thread中的ThreadStart方法?

将参数传递给Thread中的ThreadStart方法,可以通过以下几种方式实现:

  1. 使用匿名方法:
代码语言:csharp
复制
Thread thread = new Thread(delegate() {
    // 在这里编写需要在新线程中执行的代码
    Console.WriteLine("Hello from new thread!");
});
thread.Start();
  1. 使用lambda表达式:
代码语言:csharp
复制
Thread thread = new Thread(() => {
    // 在这里编写需要在新线程中执行的代码
    Console.WriteLine("Hello from new thread!");
});
thread.Start();
  1. 使用自定义类实现:
代码语言:csharp
复制
public class MyThreadClass {
    public void MyThreadMethod(string message) {
        // 在这里编写需要在新线程中执行的代码
        Console.WriteLine("Hello from new thread: " + message);
    }
}

MyThreadClass myThreadClass = new MyThreadClass();
Thread thread = new Thread(new ParameterizedThreadStart(myThreadClass.MyThreadMethod));
thread.Start("Hello World!");
  1. 使用异步方法:
代码语言:csharp
复制
public async Task MyAsyncMethod(string message) {
    // 在这里编写需要在新线程中执行的代码
    Console.WriteLine("Hello from new thread: " + message);
}

Thread thread = new Thread(async () => {
    await MyAsyncMethod("Hello World!");
});
thread.Start();

以上是四种常见的方法,可以根据实际需求选择合适的方式来实现将参数传递给Thread中的ThreadStart方法。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

没有搜到相关的沙龙

领券