loading
main.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using System;
using System.Net;

class MainClass {
  public static void Main (string[] args) 
  {
      string host = "www.kenanhancer.com";

      BlockingWithAsyncWaitHandle(host);

      BlockingWithEndingAsyncOperation(host);

      PollingStatusOfAsyncOperation(host);

      AsyncCallbackDelegateForAsyncOperation(host);
  }

  private static void BlockingWithAsyncWaitHandle(string host)
  {
      //BeginGetHostEntry starts asynchronous operation.
      IAsyncResult asyncResult = Dns.BeginGetHostEntry(host, null, null);

      Console.WriteLine($"Processing request for information in {nameof(BlockingWithAsyncWaitHandle)}");

      //Do any additional work that can be done.

      //Wait until operation completes.
      asyncResult.AsyncWaitHandle.WaitOne();
      //The operation is completed.

      //Get the result.
      IPHostEntry result = Dns.EndGetHostEntry(asyncResult);

      Console.WriteLine($"result is {result.HostName} in {nameof(BlockingWithAsyncWaitHandle)}");
  }

  private static void BlockingWithEndingAsyncOperation(string host)
  {
      //BeginGetHostEntry starts asynchronous operation.
      IAsyncResult asyncResult = Dns.BeginGetHostEntry("", null, null);

      Console.WriteLine($"Processing request for information in {nameof(BlockingWithEndingAsyncOperation)}");

      //Do any additional work that can be done.

      //EndGetHostEntry blocks until the process completes.
      IPHostEntry result = Dns.EndGetHostEntry(asyncResult);

      Console.WriteLine($"result is {result.HostName} in {nameof(BlockingWithEndingAsyncOperation)}");
  }

  private static void PollingStatusOfAsyncOperation(string host)
  {
      //BeginGetHostEntry starts asynchronous operation.
      IAsyncResult asyncResult = Dns.BeginGetHostEntry("", null, null);

      Console.WriteLine($"Processing request for information in {nameof(PollingStatusOfAsyncOperation)}");

      while (!asyncResult.IsCompleted)
      {
          //Do any additional work that can be done.
          //Example: UpdateUserInterface();
          Console.WriteLine($"waiting for {nameof(PollingStatusOfAsyncOperation)}");
      }

      //The operation is completed.
      IPHostEntry result = Dns.EndGetHostEntry(asyncResult);
  }

  private static void AsyncCallbackDelegateForAsyncOperation(string host)
  {
      AsyncCallback asyncCallback = new AsyncCallback(ProcessDnsInformation);

      // Start the asynchronous request for DNS information.
      Dns.BeginGetHostEntry(host, asyncCallback, host);

      //Do any additional work that can be done.
      Console.WriteLine($"Do some work in {nameof(AsyncCallbackDelegateForAsyncOperation)}");
  }

  private static void ProcessDnsInformation(IAsyncResult result)
  {
      string hostName = (string)result.AsyncState;

      Console.WriteLine($"Processing request for information in {nameof(AsyncCallbackDelegateForAsyncOperation)}");

      IPHostEntry host = Dns.EndGetHostEntry(result);
  }
}