WaitHandle.WaitOne Method
When overridden in a derived class, blocks the current thread until the current WaitHandle receives a signal.
Overload List
When overridden in a derived class, blocks the current thread until the current WaitHandle receives a signal.
Supported by the .NET Compact Framework.
[Visual Basic] Overloads Public Overridable Function WaitOne() As Boolean
[C#] public virtual bool WaitOne();
[C++] public: virtual bool WaitOne();
[JScript] public function WaitOne() : Boolean;
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(Integer, Boolean) As Boolean
[C#] public virtual bool WaitOne(int, bool);
[C++] public: virtual bool WaitOne(int, bool);
[JScript] public function WaitOne(int, Boolean) : Boolean;
When overridden in a derived class, blocks the current thread until the current instance receives a signal, using a TimeSpan to measure the time interval and specifying whether to exit the synchronization domain before the wait.
[Visual Basic] Overloads Public Overridable Function WaitOne(TimeSpan, Boolean) As Boolean
[C#] public virtual bool WaitOne(TimeSpan, bool);
[C++] public: virtual bool WaitOne(TimeSpan, bool);
[JScript] public function WaitOne(TimeSpan, Boolean) : Boolean;
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, C#, C++] Note This example shows how to use one of the overloaded versions of WaitOne. For other examples that might be available, see the individual overload topics.
[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(New TimeSpan(0, 0, 1), 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(new TimeSpan(0, 0, 1), 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(TimeSpan(0, 0, 1), 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
in the upper-left corner of the page.
See Also
WaitHandle Class | WaitHandle Members | System.Threading Namespace