C# Thread 启动一个进程

Thread是.net framework 1.0引入的功能

两种方式:
方式一:ThreadStart无参无返回值委托类型作为构造函数 ,只支持无返回值且无参数委托

//ThreadStart委托类型作为构造函数,只支持无返回值且无参数委托
ThreadStart threadStart = () =>
{
    Thread.Sleep(400);
    Console.WriteLine($"thread1:do something");
};
Thread thread1 = new Thread(threadStart);
thread1.Start();//开启新线程

//上面的简写
Thread thread2 = new Thread(() =>
{
    Thread.Sleep(300);
    Console.WriteLine("thread2: do something");
});
thread2.Start();

方式二:ParameterizedThreadStart无参有返回值委托类型作为构造,无返回值有参数委托

//ParameterizedThreadStart类型作为构造,无返回值有参数委托
ParameterizedThreadStart threadStart = para =>
{
    Console.WriteLine(para);
};
Thread thread3 = new Thread(threadStart);
thread3.Start("thread3:dosomething");

//上面的简写形式
Thread thread4 = new Thread(para =>
{
    Console.WriteLine(para);
});
thread4.Start("thread4:dosometing");

猜你喜欢

转载自blog.csdn.net/weixin_40719943/article/details/106972670
今日推荐