如何在 C# 线程中使用等待句柄?
我的程序中有三个线程,我希望当线程 1 完成时,它向线程 2 发出信号以启动,而当线程 2 完成时,它向线程 3 发出信号以启动。
我怎样才能实现这一点,我知道 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");
}
}
}
I have three threads in my program and I want that when thread one finishes it signals thread 2 to start and when thread 2 finishes it should signal thread 3 to start.
How can I achieve this, I know there are wait handles to do that in C#, but I don't know how to use them ?
Following is the code of my program:
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");
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您似乎想要运行任务 1 - 3 以同步执行。因此,您不妨这样做:
如果您想将这些任务的执行卸载到另一个线程,您可以这样做:
如果您确实希望每个任务在单独的线程上运行,并等待要完成上一个任务,您可以使用
Thread.Join< /code>
方法。
编辑:
由于您确实想使用等待句柄来完成此操作,请查看
ManualResetEvent
类。调用其
WaitOne
方法来等待事件,并调用Set
方法来向其发出信号。示例(极其人为的代码):
It appears that you want to run Tasks 1 - 3 to execute synchronously. So, you might as well do:
If you want to offload the execution of these tasks to another thread, you can do:
If you really wanted each task to run on a separate thread, and wait for the previous task to finish, you can use the
Thread.Join
method.EDIT:
Since you really want to use wait-handles to accomplish this, take a look at the
ManualResetEvent
class.Call the
WaitOne
method on it to wait on the event, and theSet
method to signal it.Example (horribly contrived code):
如果你想使用WaitHandles来实现这些,那么你可以执行以下操作:
声明以下两个字段:
然后在Task1的末尾,添加以下内容:
在Task2的开头,添加:
then在末尾,添加
然后最后在Task3 开头添加
If you want to use WaitHandles to acheive these then you could do the following:
declare the following two fields:
then at the end of Task1, add this:
at the beginning of Task2, add:
then at the end, add
then finally at the beginning of Task3 add
这感觉很人为,几乎就像家庭作业……
但基本上你可以在线程上使用
Join
来等待它。或者旧的 msdn 教程/示例对此非常合理: http ://msdn.microsoft.com/en-us/library/aa645740(VS.71).aspx
This feels very artificial, almost like homework...
... but basically you can use
Join
on a thread to wait for it.Or the old msdn tutorial/example is very reasonable on this: http://msdn.microsoft.com/en-us/library/aa645740(VS.71).aspx
您可以使用 ManualResetEvents 和 WaitHandle.WaitAny。基本上,当一个线程完成时,您将使用 ManualResetEvent (ManualResetEvent.Set()) 通知另一个线程。
You could use ManualResetEvents and WaitHandle.WaitAny. Basically when one thread is done you would notify the other thread by using a ManualResetEvent (ManualResetEvent.Set()).
我认为使用 thread.join() 会更简单任何其他解决方案
i think using thread.join() will be more simpler any other solution
您需要将事件传递到线程函数中,以指示每个事件完成时发出的信号以及运行之前要等待的内容。看看下面的(未经测试的)代码就明白我的意思了:
You need to pass events into the threaded functions that indicate what to signal when each one has finished and what to wait on before they run. Take a look at the (untested) code below to see what I mean: