Export (0) Print
Expand All
1 out of 3 rated this helpful - Rate this topic

WaitHandle.WaitOne Method ()

When overridden in a derived class, blocks the current thread until the current WaitHandle receives a signal.

Namespace: System.Threading
Assembly: mscorlib (in mscorlib.dll)

public virtual bool WaitOne ()
public boolean WaitOne ()
public function WaitOne () : boolean

Return Value

true if the current instance receives a signal. If the current instance is never signaled, WaitOne never returns.
Exception typeCondition

ObjectDisposedException

The current instance has already been disposed.

AbandonedMutexException

The wait completed because a thread exited without releasing a mutex. This exception is not thrown on Windows 98 or Windows Millennium Edition.

AbandonedMutexException is new in the .NET Framework version 2.0. In previous versions, the WaitOne method returns true when a mutex is abandoned. An abandoned mutex indicates a serious coding error. The exception contains information useful for debugging.

The caller of this method blocks indefinitely until the current instance receives a signal. 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 IAsyncResult interface.

Calling this method overload is equivalent to calling the WaitOne(Int32,Boolean) method overload and specifying -1 or Timeout.Infinite for the first parameter and false for the second parameter.

Override this method to customize the behavior of derived classes.

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.

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.
        autoEvent.WaitOne();
        Console.WriteLine("Work method signaled.\nMain 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();
    }
}

import System.*;
import System.Threading.*;
import System.Threading.Thread;

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

    public static void main(String[] args)
    {
        Console.WriteLine("Main starting.");
        ThreadPool.QueueUserWorkItem(new WaitCallback(WorkMethod), autoEvent);

        // Wait for work method to signal.
        autoEvent.WaitOne();
        Console.WriteLine("Work method signaled.\nMain ending.");
    } //main

    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();
    } //WorkMethod
} //WaitOne

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.