Last active
September 27, 2018 21:17
-
-
Save kenanhancer/a1bed02357ab2925a99b223c66f24416 to your computer and use it in GitHub Desktop.
EventBasedAsynchronousPattern_EAP created by kenanhancer - https://repl.it/@kenanhancer/EventBasedAsynchronousPatternEAP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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