我在我的程序中有三个线程,我希望当线程完成时它表示线程2启动,当线程2完成时它应该指示线程3启动。如何在C#线程中使用等待句柄?
我该怎么做到这一点,我知道在C#中有这样的等待句柄,但我不知道如何使用它们?
以下是我的程序的代码:
class Program
{
static void Main(string[] args)
{
Thread t1 = new Thread(Task1);
Thread t2 = new Thread(Task2);
Thread t3 = new Thread(Task3);
t1.Start();
t2.Start();
t3.Start();
Console.Read();
}
public static void Task1()
{
Console.WriteLine("I am assigned task 1:");
for (int i = 0; i < 50; i++)
{
Console.WriteLine("Task1");
}
}
public static void Task2()
{
Console.WriteLine("I am assigned task 2:");
for (int i = 0; i < 50; i++)
{
Console.WriteLine("Task2");
}
}
public static void Task3()
{
Console.WriteLine("I am assigned task 3:");
for (int i = 0; i < 50; i++)
{
Console.WriteLine("Task3");
}
}
}
你可以在't1.Start'中传递't2'作为参数,然后在'Task1'结尾使用你得到的参数调用't2.Start()'。为此,请改用[ParameterizedThreadStart](http://msdn.microsoft.com/zh-cn/library/system.threading.parameterizedthreadstart.aspx)。 – 2011-04-04 13:14:47