返回介绍

使用 AsyncWaitHandle 阻止应用程序的执行

发布于 2025-02-23 23:15:47 字数 4568 浏览 0 评论 0 收藏 0

操作完成之前,必须阻止的应用程序无法继续执行其他工作的同时等待异步操作的结果。 使用以下选项之一来阻止应用程序的主线程在等待异步操作以完成时:

  • 使用 AsyncWaitHandle 属性 IAsyncResult 异步操作返回 开始 OperationName方法。 本主题中演示了这种方法。
  • 调用异步操作的 结束 OperationName方法。 有关演示此方法的示例,请参阅 通过结束异步操作来阻止应用程序执行 。

使用一个或多个应用程序 WaitHandle 对象进行阻止,直到异步操作已完成,通常会调用 开始 OperationName方法,执行可以完成任何工作而无需操作,和之前的异步操作的块的结果完成。 应用程序可以通过调用其中一种来阻止一个操作上 WaitOne 方法使用 AsyncWaitHandle 。 若要阻止等待异步操作完成的一组时,存储关联 AsyncWaitHandle 中数组中,然后调用的对象 WaitAll 方法。 若要阻止等待任一一套异步操作完成时,存储关联 AsyncWaitHandle 中数组中,然后调用的对象 WaitAny 方法。

示例

下面的代码示例演示如何使用 DNS 类中的异步方法来检索用户指定的计算机的域名系统信息。 示例演示了阻止使用 WaitHandle 与异步操作关联。 请注意, null ( Nothing 在 Visual Basic 中) 为传递 BeginGetHostByName requestCallbackstateObject 参数因为使用此方法时,这些并不需要。

/*
The following example demonstrates using asynchronous methods to
get Domain Name System information for the specified host computer.

*/

using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace Examples.AdvancedProgramming.AsynchronousOperations
{
  public class WaitUntilOperationCompletes
  {
    public static void Main(string[] args)
    {
      // Make sure the caller supplied a host name.
      if (args.Length == 0 || args[0].Length == 0)
      {
        // Print a message and exit.
        Console.WriteLine("You must specify the name of a host computer.");
        return;
      }
      // Start the asynchronous request for DNS information.
      IAsyncResult result = Dns.BeginGetHostEntry(args[0], null, null);
      Console.WriteLine("Processing request for information...");
      // Wait until the operation completes.
      result.AsyncWaitHandle.WaitOne();
      // The operation completed. Process the results.
      try 
      {
        // Get the results.
        IPHostEntry host = Dns.EndGetHostEntry(result);
        string[] aliases = host.Aliases;
        IPAddress[] addresses = host.AddressList;
        if (aliases.Length > 0)
        {
          Console.WriteLine("Aliases");
          for (int i = 0; i < aliases.Length; i++)
          {
            Console.WriteLine("{0}", aliases[i]);
          }
        }
        if (addresses.Length > 0)
        {
          Console.WriteLine("Addresses");
          for (int i = 0; i < addresses.Length; i++)
          {
            Console.WriteLine("{0}",addresses[i].ToString());
          }
        }
      }
      catch (SocketException e)
      {
        Console.WriteLine("Exception occurred while processing the request: {0}", 
          e.Message);
      }
    }
  }
}
' The following example demonstrates using asynchronous methods to
' get Domain Name System information for the specified host computer.

Imports System
Imports System.Net
Imports System.Net.Sockets
Imports System.Threading

namespace Examples.AdvancedProgramming.AsynchronousOperations
  Public Class WaitUntilOperationCompletes
  
    Public Shared Sub Main(args() as String)
      ' Make sure the caller supplied a host name.
      If(args.Length = 0)
        ' Print a message and exit.
        Console.WriteLine("You must specify the name of a host computer.")
        End
      End If
      ' Start the asynchronous request for DNS information.
      Dim result as IAsyncResult= Dns.BeginGetHostEntry(args(0), Nothing, Nothing)
      Console.WriteLine("Processing request for information...")
      ' Wait until the operation completes.
      result.AsyncWaitHandle.WaitOne()
      ' The operation completed. Process the results.
      Try 
        ' Get the results.
        Dim host as IPHostEntry = Dns.EndGetHostEntry(result)
        Dim  aliases() as String = host.Aliases
        Dim addresses() as IPAddress= host.AddressList
        Dim i as Integer
        If aliases.Length > 0
          Console.WriteLine("Aliases")
          For i = 0 To aliases.Length -1 
            Console.WriteLine("{0}", aliases(i))
          Next i
        End If
        If addresses.Length > 0
          Console.WriteLine("Addresses")
          For i = 0 To addresses.Length -1
            Console.WriteLine("{0}", addresses(i).ToString())
          Next i
        End If
      Catch e as SocketException
          Console.WriteLine("An exception occurred while processing the request: {0}" _
          , e.Message)
      End Try
    End Sub
  End Class
End Namespace

另请参阅

基于事件的异步模式 (EAP)
基于事件的异步模式概述

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文