Click here to Skip to main content
15,878,809 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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 ?
Posted
Updated 14-Oct-14 17:19pm
v4
Comments
[no name] 12-Oct-14 8:44am    
http://www.codeproject.com/Answers/827995/how-to-release-thread-is-required-in-multiple-thre#answer1
pasztorpisti 14-Oct-14 11:26am    
In a well designed multithreaded system you almost never need the direct usage of an event object, same is true for most low level synchronization primitives. Are you here to learn about the usage of synchronization primitives or to solve your particular gui related problem? (Because synchronization of the gui and background threads is done differently.)

1 solution

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
 
Share this answer
 
Comments
sergio090588 14-Oct-14 11:13am    
I updated my code
pleae, check it and help me !!
thank you so much
Sergey Alexandrovich Kryukov 14-Oct-14 12:02pm    
I see. I already answered on how to selectively release some of the threads.
As to the purpose, it still remains mysterious to me. Now it is also related to the UI of the site and Monitor. Pulse passes exactly one thread, first in the queue. Why would you need that?
—SA
sergio090588 14-Oct-14 14:04pm    
- First, I use manualresetevent but call Set() all backgroundworker is waiting running. Now, I use Monitor.wait()/Pulse() approximately my ideal but still a litle issue.
- I read your article but really I don't understand cause I just have known about thread
please, you can guide me to fix my code !!
Sergey Alexandrovich Kryukov 14-Oct-14 14:15pm    
I already answered: if you want to do what you want, use separate EventWaitHandle for each thread.
If you did not understand my article, you are welcome to ask questions.
To do what you do, you have to learn threading more throughly.
—SA
sergio090588 14-Oct-14 14:24pm    
EventWaitHandle?
I read it and it is parent of manualresetevent/autoresetevent

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900