如何手动重复工作? [英] how manualresetevent work ?

查看:74
本文介绍了如何手动重复工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!

有人可以向我解释一下manualresetevent如何在多个背景工作者中工作!



- 我尝试调试并看到:

当我在多个后台工作者中调用waitone()时所有线程都会等待,当我调用set()时所有线程都在等待运行。如何运行线程选择和其他线程仍在等待?

Hi everybody !
somebody can explain for me how manualresetevent work in multi backgroundworker !

- I try to debug and see:
when I call waitone() in multi backgroundworker so all thread will wait and when I call set() so all thread is waiting will run. How way to run thread selection and other threads still waiting ?

推荐答案

首先,正确的问题会询问线程,而不是 BackgroundWorker ,这只是创建单独帖子的方法之一。



这不是<$ b>的目的 c $ c> System.Threading.EventWaitHandle (手动或自动重置只是选项或重置;此类派生为两个类, System.Threading.ManualResetEvent System.Threading.AutoResetEvent 仅用于嵌入重置的差异;如果需要,可以直接使用 System.Threading。 EventWaitHandle 所有功能都在哪里,所以我们只描述这个类。



所以,等待线程的状态调用<$ c $之一c> EventWaitHandle.WaitOne 方法可以以重置调用中使用的等待句柄实例结束。(唤醒线程的其他原因是 Abort In terrupt ,以及(可选)超时到期时间。如果您使用 Set 发出事件句柄的信号,队列中的所有线程将被释放。



因此,所有你可以做的就是选择性唤醒一个线程,它将所有线程保持在不同等待处理程序实例的不同等待队列中,每个线程一个等待处理程序。



这将是解决方案,但它让我关注整个目的。您很可能需要查看线程代码的设计。



-SA
First of all, correct question would ask about threads, not BackgroundWorker, which is just one of the ways to create a separate thread.

This is not the purpose of System.Threading.EventWaitHandle (manual or auto-reset are just to options or reset; this class is derived into two classes, System.Threading.ManualResetEvent and System.Threading.AutoResetEvent only to embed the difference in reset; if you want, you can directly use System.Threading.EventWaitHandle where all functionality is, so let's describe only this class.

So, wait state of a thread calling one of EventWaitHandle.WaitOne methods can end with resetting of the instance of the wait handle used in the call. (Other reasons for waking up the thread are Abort, Interrupt and, optionally, the timeout expiration). If you signal the event handle with Set, all threads in the queue will be released.

Therefore, all you can do to have selective wake up of a thread, it to keep all threads in different wait queues of different instances of wait handlers, one wait handler per thread.

This would be the solution, but it makes me concerned about the whole purpose. It's quite possible that you rather need to review the design of your threaded code.

—SA


这篇关于如何手动重复工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆