WaitHandle.WaitOne Method (Int32)

July 28, 2014

Blocks the current thread until the current WaitHandle receives a signal, using 32-bit signed integer to specify the time interval.

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

'Declaration
Public Overridable Function WaitOne ( _
	millisecondsTimeout As Integer _
) As Boolean

Parameters

millisecondsTimeout
Type: System.Int32
The number of milliseconds to wait, or Timeout.Infinite (-1) to wait indefinitely.

Return Value

Type: System.Boolean
true if the current instance receives a signal; otherwise, false.

ExceptionCondition
ObjectDisposedException

The current instance has already been disposed.

ArgumentOutOfRangeException

millisecondsTimeout is a negative number other than -1, which represents an infinite time-out.

If millisecondsTimeout is zero, the method does not block. It tests the state of the wait handle and returns immediately.

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 the signal that is generated when an asynchronous operation completes. For more information, see the IAsyncResult interface.

Override this method to customize the behavior of derived classes.

The following example shows how to use the WaitOne(Int32) method overload to enable the waiting thread to break out of a long wait and do some processing.

The example queues three tasks by using the ThreadPool.QueueUserWorkItem method. Each task calls WaitOne on a common AutoResetEvent, using a three-second time-out. If the wait times out, the task displays a message and resumes waiting. You could add code here to abandon the wait depending on conditions.

The example releases a waiting thread each time the MouseLeftButtonUp event occurs. When the last thread is released, it unhooks the event handler and displays a final message. The Interlocked.Increment and Interlocked.Decrement methods are used to ensure that the count is protected from concurrent access.

The example uses helper methods, executed on the user interface thread by the Dispatcher.BeginInvoke method, to safely access user interface elements from ThreadPool threads.


Imports System.Threading

' The following imports simplify the supporting code; they are not required for 
' WaitHandle:
Imports System.Windows.Controls
Imports System.Windows.Input

Public Class Example

   Private Shared outputBlock As System.Windows.Controls.TextBlock
   Private Shared autoEvent As New AutoResetEvent(False)
   Private Shared threadCount As Integer = 0

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      Example.outputBlock = outputBlock

      For i As Integer = 1 To 3
         Interlocked.Increment(threadCount)
         ThreadPool.QueueUserWorkItem(AddressOf WorkMethod, i)
      Next i

      AddHandler outputBlock.MouseLeftButtonUp, AddressOf MouseUp

   End Sub

   Private Shared Sub WorkMethod(ByVal stateInfo As Object)

      Dim number As Integer = CInt(stateInfo)
      outputBlock.Dispatcher.BeginInvoke(displayHelper, _
                 String.Format("Task {0} is queued. Click to release." & vbLf, number))

      Dim start As DateTime = DateTime.Now
      While Not autoEvent.WaitOne(3000)
         outputBlock.Dispatcher.BeginInvoke(displayHelper, _
            String.Format("Task {0} has waited {1} seconds. Click to release." & vbLf, _
                          number, _
                          (DateTime.Now - start).TotalMilliseconds))
      End While

      outputBlock.Dispatcher.BeginInvoke(displayHelper, _
                 String.Format("Task {0} is ending." & vbLf, number))

      If Interlocked.Decrement(threadCount) = 0 Then

         ' Disable the mouse.
         outputBlock.Dispatcher.BeginInvoke(unhookHelper)

         outputBlock.Dispatcher.BeginInvoke(displayHelper, _
                              "Refresh the page to run the demo again." & vbLf)

      End If
   End Sub

   Private Shared Sub MouseUp(ByVal sender As Object, ByVal e As MouseButtonEventArgs)

      ' Release a thread.
      autoEvent.Set()

   End Sub

   ' Helper methods:

   ' In order to update the TextBlock object, which is on the UI thread, you must
   ' make a cross-thread call by using the Dispatcher object that is associated 
   ' with the TextBlock. The DisplayOutput helper method and its delegate, 
   ' displayHelper, are used by the BeginInvoke method of the Dispatcher object
   ' to append text to the TextBlock. UnhookMouseUp and its delegate, unhookHelper,
   ' unhook the event handler for the MouseLeftButtonUp event.
   '
   Private Shared displayHelper As New Action(Of String)(AddressOf DisplayOutput)
   Private Shared Sub DisplayOutput(ByVal msg As String)
      outputBlock.Text &= msg 
   End Sub

   Private Shared unhookHelper As New Action(AddressOf UnhookMouseUp)
   Private Shared Sub UnhookMouseUp()
      RemoveHandler outputBlock.MouseLeftButtonUp, AddressOf MouseUp
   End Sub

End Class

' This example produces output similar to the following:
'
'Task 1 is queued. Click to release.
'Task 2 is queued. Click to release.
'Task 3 is queued. Click to release.
'Task 2 has waited 3010.7421 seconds. Click to release.
'Task 1 has waited 3026.3418 seconds. Click to release.
'Task 3 has waited 3010.7421 seconds. Click to release.
'Task 3 is ending.
'Task 1 has waited 6037.0839 seconds. Click to release.
'Task 2 has waited 6021.4842 seconds. Click to release.
'Task 2 is ending.
'Task 1 is ending.
'Refresh the page to run the demo again.


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2014 Microsoft