请帮我写一个c sharp多线程示例
时间: 2023-05-12 07:05:10 浏览: 174
多线程实例
当然,我很乐意帮你写一个C#多线程示例。以下是一个简单的示例代码:
using System;
using System.Threading;
class Program
{
static void Main(string[] args)
{
// 创建两个线程
Thread t1 = new Thread(new ThreadStart(DoWork1));
Thread t2 = new Thread(new ThreadStart(DoWork2));
// 启动线程
t1.Start();
t2.Start();
// 等待线程结束
t1.Join();
t2.Join();
Console.WriteLine("All threads finished.");
Console.ReadKey();
}
static void DoWork1()
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine("Thread 1: " + i);
Thread.Sleep(100);
}
}
static void DoWork2()
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine("Thread 2: " + i);
Thread.Sleep(100);
}
}
}
希望这个示例能够帮到你。
阅读全文