Skip to content

Instantly share code, notes, and snippets.

@kenanhancer
Last active September 27, 2018 21:17
Show Gist options
  • Save kenanhancer/a1bed02357ab2925a99b223c66f24416 to your computer and use it in GitHub Desktop.
Save kenanhancer/a1bed02357ab2925a99b223c66f24416 to your computer and use it in GitHub Desktop.
EventBasedAsynchronousPattern_EAP created by kenanhancer - https://repl.it/@kenanhancer/EventBasedAsynchronousPatternEAP
using System;
using System.Net;
using System.Threading.Tasks;
class MainClass {
public static void Main (string[] args) {
CustomDnsWithEAP.GetHostEntryCompleted += CustomDnsWithEAP_GetHostEntryCompleted;
CustomDnsWithEAP.GetHostEntryAsync("www.kenanhancer.com");
CustomDnsWithEAP.GetHostEntryAsync("github.com");
Console.ReadLine();
}
private static void CustomDnsWithEAP_GetHostEntryCompleted(object sender, CustomDnsEventArgs<IPHostEntry> e)
{
Console.WriteLine(e.Result.HostName);
}
}
public class CustomDnsEventArgs<TResult> : EventArgs
{
public TResult Result
{
get;
private set;
}
public CustomDnsEventArgs(TResult result)
{
Result = result;
}
}
public static class CustomDnsWithEAP
{
public static event EventHandler<CustomDnsEventArgs<IPHostEntry>> GetHostEntryCompleted;
private static void RaiseGetHostEntryCompleted(IPHostEntry result)
{
var handler = GetHostEntryCompleted;
if (handler != null)
{
handler(null, new CustomDnsEventArgs<IPHostEntry>(result));
}
}
public static void GetHostEntryAsync(string hostNameOrAddress)
{
Task.Run(() =>
{
IPHostEntry host = Dns.GetHostEntry(hostNameOrAddress);
RaiseGetHostEntryCompleted(host);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment