new AutoResetEvent (true) Usages in C#?
The scenario would be that the first thread that calls WaitOne
should immediately pass through, without blocking.
Check the Silverlight documentation for AutoResetEvent (strangely enough the doc is not the same on the .Net versions):
Specifying
true
forinitialState
creates anAutoResetEvent
in the signaled state. This is useful if you want the first thread that waits for theAutoResetEvent
to be released immediately, without blocking."
There's a good explanation here: Signaling with Event Wait Handles.
To paraphrase, the wait handle is like a turnstile, with the callers of WaitOne
being like a line of people queuing at the turnstile. Each time Set
is called, the turnstile allows one person through (usually in the order that they queued, but sometimes not, due to OS quirks).
If it is constructed with 'true', then the turnstile is already open, but for only one person, so that the first caller of WaitOne
will be 'let through' straight away, but any subsequent callers will still have to queue.
Obviously none of this would be applicable if you only have one caller of WaitOne
, and so the 'true' option would be moot.
Sure.
One requirement - a thread X must run on app start and every 10 secs thereafter. A timed WaitForSingleObject(myARE,10000) would do the job. Initializing the ARE to 'true' would ensure the thread runs on startup.
Edit:
myARE.WaitOne(10000);
Sorry - I dropped out of C# mode into WINAPI, briefly :(