信息
您所需的主题如下所示。但此主题未包含在此库中。

WaitHandle.WaitOne 方法 (Int32)

2013/12/13

阻止当前线程,直到当前 WaitHandle 收到信号,同时使用 32 位有符号整数指定时间间隔。

Namespace:  System.Threading
程序集:  mscorlib(位于 mscorlib.dll 中)

public virtual bool WaitOne(
	int millisecondsTimeout
)

参数

millisecondsTimeout
类型: System.Int32
等待的毫秒数,或为 Timeout.Infinite (-1),表示无限期等待。

返回值

类型: System.Boolean
如果当前实例收到信号,则为 true;否则为 false

异常条件
ObjectDisposedException

当前实例已被释放。

ArgumentOutOfRangeException

millisecondsTimeout 是一个非 -1 的负数,而 -1 表示无限期超时。

如果 millisecondsTimeout 为零,则该方法不会进入阻止状态。该方法会测试等待句柄的状态并立即返回。

该方法的调用方在当前实例收到信号或发生超时前将被阻止。使用此方法阻止,直到 WaitHandle 收到来自另一个线程的信号,例如异步操作完成时生成的信号。有关更多信息,请参见 IAsyncResult 接口。

重写此方法以自定义派生类的行为。

下面的示例演示如何使用 WaitOne(Int32) 方法重载,使等待线程可以跳出长时间等待并进行一些处理。

该示例使用 ThreadPool.QueueUserWorkItem 方法对三个任务进行排队。每个任务在公共 AutoResetEvent 上调用 WaitOne,并使用三秒的超时期限。如果等待超时,则任务将显示一条消息并恢复等待。您可将代码添加到此处以根据条件放弃等待。

该示例在每次发生 MouseLeftButtonUp 事件时释放一个等待线程。释放最后一个线程时,它将解除事件处理程序挂钩并显示最终消息。使用 Interlocked.IncrementInterlocked.Decrement 方法确保计数不会受到同时访问。

该示例使用帮助器方法(由 Dispatcher.BeginInvoke 方法在用户界面线程上执行),从 ThreadPool 线程安全访问用户界面元素。

说明注意:

要运行此示例,请参见生成具有静态 Windows Phone TextBlock 控件的示例


using System;
using System.Threading;

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

public class Example
{
   private static System.Windows.Controls.TextBlock outputBlock;
   private static AutoResetEvent autoEvent = new AutoResetEvent(false);
   private static int threadCount = 0;

   public static void Demo(TextBlock outputBlock)
   {
      Example.outputBlock = outputBlock;

      for(int i = 1; i <= 3; i++)
      {
         Interlocked.Increment(ref threadCount);
         ThreadPool.QueueUserWorkItem(WorkMethod, i);
      }

      outputBlock.MouseLeftButtonUp += MouseUp;
   }

   private static void WorkMethod(object stateInfo)
   {
      int number = (int) stateInfo;
      outputBlock.Dispatcher.BeginInvoke(displayHelper, 
            String.Format("Task {0} is queued. Click to release.\n", number));

      DateTime start = DateTime.Now;
      while (!autoEvent.WaitOne(3000))
      {
         outputBlock.Dispatcher.BeginInvoke(displayHelper, 
            String.Format("Task {0} has waited {1} seconds. Click to release.\n", 
                          number, 
                          (DateTime.Now-start).TotalMilliseconds));
      }

      outputBlock.Dispatcher.BeginInvoke(displayHelper, 
                     String.Format("Task {0} is ending.\n", number));

      if (Interlocked.Decrement(ref threadCount) == 0)
      {
         // Disable the mouse.
         outputBlock.Dispatcher.BeginInvoke(delegate () {
                                   outputBlock.MouseLeftButtonUp -= MouseUp; });

         outputBlock.Dispatcher.BeginInvoke(displayHelper, 
                         "Refresh the page to run the demo again.\n");               
      }
   }

   private static void MouseUp(object sender, MouseButtonEventArgs e)
   {
      // Release a thread.
      autoEvent.Set();
   }

   // 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. 
   //
   private static Action<string> displayHelper = new Action<string>(DisplayOutput);
   private static void DisplayOutput(string msg)
   {
      outputBlock.Text += msg;
   }
}

/* 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

受以下版本支持: 8.0, 7.1, 7.0

Windows Phone

显示: