WaitOne question

  • Thread starter Thread starter csharpula csharp
  • Start date Start date
C

csharpula csharp

Hello I am using the ManualResetEvent WaitOne and I have a question:
I use it in order to wait for sometimeout and if some function wasn't
activated by other process I should activate it myself.
How can I know if some function was called during the Wait?
Thanks!
 
The WaitOne overload with a timeout returns a boolean to indicate whether
you are returning because of activation or timeout; is this what you are
looking for?

Marc
 
csharpula said:
Hello I am using the ManualResetEvent WaitOne and I have a question:
I use it in order to wait for sometimeout and if some function wasn't
activated by other process I should activate it myself.
How can I know if some function was called during the Wait?
Thanks!

Here are some sample code from MSDN :


class WaitOne
{
static AutoResetEvent autoEvent = new AutoResetEvent(false);

static void Main()
{
Console.WriteLine("Main starting.");

ThreadPool.QueueUserWorkItem(
new WaitCallback(WorkMethod), autoEvent);

// Wait for work method to signal.
if(autoEvent.WaitOne(1000, false))
{
Console.WriteLine("Work method signaled.");
}
else
{
Console.WriteLine("Timed out waiting for work " +
"method to signal.");

** You can set the event here by yourself **
}
Console.WriteLine("Main ending.");
}

static void WorkMethod(object stateInfo)
{
Console.WriteLine("Work starting.");

// Simulate time spent working.
Thread.Sleep(new Random().Next(100, 2000));

// Signal that work is finished.
Console.WriteLine("Work ending.");
((AutoResetEvent)stateInfo).Set();
}
}
 
Back
Top