Export (0) Print
Expand All

WaitHandle.WaitOne Method (Int32, Boolean)

.NET Framework 1.1

When overridden in a derived class, blocks the current thread until the current WaitHandle receives a signal, using 32-bit signed integer to measure the time interval and specifying whether to exit the synchronization domain before the wait.

[Visual Basic]
Overloads Public Overridable Function WaitOne( _
   ByVal millisecondsTimeout As Integer, _
   ByVal exitContext As Boolean _
) As Boolean
[C#]
public virtual bool WaitOne(
 int millisecondsTimeout,
 bool exitContext
);
[C++]
public: virtual bool WaitOne(
 int millisecondsTimeout,
 bool exitContext
);
[JScript]
public function WaitOne(
   millisecondsTimeout : int,
 exitContext : Boolean
) : Boolean;

Parameters

millisecondsTimeout
The number of milliseconds to wait, or Timeout.Infinite (-1) to wait indefinitely.
exitContext
true to exit the synchronization domain for the context before the wait (if in a synchronized context), and reacquire it; otherwise, false.

Return Value

true if the current instance receives a signal; otherwise, false.

Exceptions

Exception Type Condition
ObjectDisposedException The current instance has already been disposed.
ArgumentOutOfRangeException millisecondsTimeout is a negative number other than -1, which represents an infinite time-out.

Remarks

The caller of this method blocks until the current instance receives a signal or a time-out occurs. Use this method to block until a WaitHandle receives a signal from another thread, such as is generated when an asynchronous operation completes. For more information, see the System.IAsyncResult interface.

Override this method to customize the behavior of derived classes.

Example

[Visual Basic, C#, C++] The following code example shows how to use a wait handle to keep a process from terminating while it waits for a background thread to finish executing.

[Visual Basic] 
Imports System
Imports System.Threading

Public Class WaitOne

    Shared autoEvent As New AutoResetEvent(False)

    Shared Sub Main()
        Console.WriteLine("Main starting.")

        ThreadPool.QueueUserWorkItem(AddressOf WorkMethod, autoEvent)

        ' Wait for work method to signal.
        If autoEvent.WaitOne(1000, False) Then
            Console.WriteLine("Work method signaled.")
        Else
            Console.WriteLine("Timed out waiting for work " & _
                "method to signal.")
        End If

        Console.WriteLine("Main ending.")
    End Sub

    Shared Sub WorkMethod(stateInfo As Object) 
        Console.WriteLine("Work starting.")

        ' Simulate time spent working.
        Thread.Sleep(New Random().Next(100, 2000))

        ' Signal that work is finished.
        Console.WriteLine("Work ending.")
        CType(stateInfo, AutoResetEvent).Set()
    End Sub

End Class

[C#] 
using System;
using System.Threading;

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.");
        }
        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();
    }
}

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::Threading;

__gc class WaitOne
{
    WaitOne() {}

public:
    static void WorkMethod(Object* stateInfo) 
    {
        Console::WriteLine(S"Work starting.");

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

        // Signal that work is finished.
        Console::WriteLine(S"Work ending.");
        dynamic_cast<AutoResetEvent*>(stateInfo)->Set();
    }
};

void main()
{
    Console::WriteLine(S"Main starting.");

    AutoResetEvent* autoEvent = new AutoResetEvent(false);

    ThreadPool::QueueUserWorkItem(
        new WaitCallback(0, &WaitOne::WorkMethod), autoEvent);

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

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

WaitHandle Class | WaitHandle Members | System.Threading Namespace | WaitHandle.WaitOne Overload List

Show:
© 2015 Microsoft