c# - When should I use a WaitHandle instead of a lock -


in c#, when should use waithandle instead of lock ?

a lock fundamentally different waithandle. lock write, example:

lock (someresource) {     // stuff here } 

the first line says, "i want exclusive access resource," , wait until resource available (i.e. nobody else has lock on it). code runs , @ end lock released others can use it.

waithandle used wait event take place. when write:

mywaithandle.waitone(); 

your code waiting other thread (possibly in different process) signal waithandle. signals anything. perhaps you're waiting thread finish job , say, "i'm done now," before code can continue.

so, answer question, should use lock when want gain exclusive access resource. should use waithandle when want notified of event.


Comments