Questions tagged [waithandle]

In .Net, WaitHandles are used for synchronization among threads. They contain a handle to the OS's synchronization primitive. Mutex, Semaphore, and EventWaitHandle are exemples of classes that inherit from WaitHandle.

See http://www.albahari.com/threading/part2.aspx for a tour of the available synchronization constructs in .Net.

113 questions
54
votes
8 answers

Workaround for the WaitHandle.WaitAll 64 handle limit?

My application spawns loads of different small worker threads via ThreadPool.QueueUserWorkItem which I keep track of via multiple ManualResetEvent instances. I use the WaitHandle.WaitAll method to block my application from closing until these…
James
  • 80,725
  • 18
  • 167
  • 237
45
votes
4 answers

How to check if the WaitHandle was set?

I have a WaitHandle and I would like to know how to check if the WaitHandle has already been set or not. Note: I can add a bool variable and whenever Set() method is used set the variable to true, but this behaviour must be built in WaitHandle…
MartyIX
  • 27,828
  • 29
  • 136
  • 207
17
votes
6 answers

What is the difference between Thread.Sleep(timeout) and ManualResetEvent.Wait(timeout)?

Both Thread.Sleep(timeout) and resetEvent.Wait(timeout) cause execution to pause for at least timeout milliseconds, so is there a difference between them? I know that Thread.Sleep causes the thread to give up the remainder of its time slice, thus…
Erik Forbes
  • 35,357
  • 27
  • 98
  • 122
16
votes
6 answers

Do I need to call Close() on a ManualResetEvent?

I've been reading up on .NET Threading and was working on some code that uses a ManualResetEvent. I have found lots of code samples on the internet. However, when reading the documentation for WaitHandle, I saw the following: WaitHandle…
Kevin Hakanson
  • 41,386
  • 23
  • 126
  • 155
15
votes
2 answers

What does the exit context mean for a WaitHandle.WaitOne mean?

I'm trying to use a mutex to protect access to some hardware from multiple threads, but I'm confused as to what the exitContext parameter means / does: public virtual bool WaitOne ( int millisecondsTimeout, bool exitContext ) The docs…
Jon Cage
  • 36,366
  • 38
  • 137
  • 215
15
votes
2 answers

My EventWaitHandle says "Access to the path is denied", but its not

Quick summary with what I now know I've got an EventWaitHandle that I created and then closed. When I try to re-create it with this ctor, an "Access to the path ... is denied" exception is thrown. This exception is rare, most of the times it just…
Allen Rice
  • 19,068
  • 14
  • 83
  • 115
14
votes
2 answers

When can ManualResetEvent.Set() return false?

According the the MSDN documentation, Set() and Reset() on ManualResetEvent (or any EventWaitHandle) returns a boolean indicator whether or not the operation was successful. Under which circumstances can this call return false, and what am I…
SoftMemes
  • 5,602
  • 4
  • 32
  • 61
13
votes
3 answers

AutoResetEvent.WaitOne with timeout vs Thread.Sleep

I need a solution to perform arbitrary pause. The delay accuracy is irrelevant. What is the practical difference in such scenario between WaitHandle.WaitOne Method (TimeSpan) and Thread.Sleep Method. Are there any better solutions?
Ryszard Dżegan
  • 24,366
  • 6
  • 38
  • 56
13
votes
2 answers

AutoResetEvent as a Lock replacement in C#?

I was wondering: Locking allows only 1 thread to enter a code region And wait handles is for signaling : : Signaling is when one thread waits until it receives notification from another. So I thought to myself , can this be used to replace a…
Royi Namir
  • 144,742
  • 138
  • 468
  • 792
12
votes
3 answers

How do I unblock threads which have called the WaitOne method on an AutoResetEvent object?

Below is a class having the method 'SomeMethod' that illustrates my problem. class SomeClass { AutoResetEvent theEvent = new AutoResetEvent(false); // more member declarations public void SomeMethod() { // some code …
ghd
  • 258
  • 1
  • 3
  • 14
9
votes
4 answers

How do I use WaitHandler.WaitAll in MSTest without STA warnings?

Is there a way to unit test WaitHandle.WaitAll() when using Visual Studio's built-in unit testing solution. When I try and run a test that uses this function within Visual Studio the test fails and when examining the test results the following…
RobV
  • 28,022
  • 11
  • 77
  • 119
9
votes
2 answers

What's the proper way to wait on a Semaphore?

I thought that the following code would let all the 10 threads run, two at a time, and then print "done" after Release() is called 10 times. But that's not what happened: int count = 0; Semaphore s = new Semaphore(2, 2); for…
John Smith
  • 4,416
  • 7
  • 41
  • 56
8
votes
4 answers

How do you close an application when some WaitHandle is in the middle of a call to WaitOne?

Is there a standard way to close out an application "cleanly" while some WaitHandle objects may be in the state of a current blocking call to WaitOne? For example, there may be a background thread that is spinning along in a method like this: while…
Dan Tao
  • 125,917
  • 54
  • 300
  • 447
8
votes
3 answers

Async/await tasks and WaitHandle

Say I have 10N items(I need to fetch them via http protocol), in the code N Tasks are started to get data, each task takes 10 items in sequence. I put the items in a ConcurrentQueue. After that, the items are processed in a thread-unsafe…
Cheng Chen
  • 42,509
  • 16
  • 113
  • 174
7
votes
1 answer

Explanation of Text on Threading in "C# 3.0 in a Nutshell"

While reading C# 3.0 in a Nutshell by Joseph and Ben Albahari, I came across the following paragraph (page 673, first paragraph in section titled "Signaling with Wait and Pulse") "The Monitor class provides another signalling construct via two…
RAL
  • 917
  • 8
  • 19
1
2 3 4 5 6 7 8