在C# WinForms中,可以使用System.Threading.Thread
类或者System.Threading.Tasks.Task
类来创建多线程。下面是两种方法的示例:
方法1:使用System.Threading.Thread类
System.Threading.Thread
的类:public class MyThread : Thread
{
public void RunThread()
{
// 在这里编写你的线程代码
}
}
private void button1_Click(object sender, EventArgs e)
{
MyThread myThread = new MyThread();
myThread.RunThread();
}
方法2:使用System.Threading.Tasks.Task类
private void button1_Click(object sender, EventArgs e)
{
Task.Run(() =>
{
// 在这里编写你的线程代码
});
}
这两种方法都可以实现在WinForms中启动多线程。System.Threading.Tasks.Task
类更适合处理异步操作,因为它提供了更多的功能和更好的性能。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:C# WinForm多线程编程指南