Skip to content

Instantly share code, notes, and snippets.

@kenanhancer
Last active September 27, 2018 17:39
Show Gist options
  • Save kenanhancer/27a74760a855ca770b644613b7de8a5f to your computer and use it in GitHub Desktop.
Save kenanhancer/27a74760a855ca770b644613b7de8a5f to your computer and use it in GitHub Desktop.
AsynchronousProgrammingModel(APN) created by kenanhancer - https://repl.it/@kenanhancer/AsynchronousProgrammingModelAPN
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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment