loading
open in
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
using System;
using System.Net;
using System.Threading;
using System.Threading.Tasks;

class MainClass {
  private static SemaphoreSlim _throttle = new SemaphoreSlim(2);

  public static void Main (string[] args) {

    Console.WriteLine($"Main Thread ID: {Thread.CurrentThread.ManagedThreadId}");

    CustomDnsWithEAP.GetHostEntryCompleted += CustomDnsWithEAP_GetHostEntryCompleted;

    _throttle.Wait();

    CustomDnsWithEAP.GetHostEntryAsync("www.kenanhancer.com");

    _throttle.Wait();

    CustomDnsWithEAP.GetHostEntryAsync("www.github.com");

    while (_throttle.CurrentCount != 2)
    {
        Thread.Sleep(1000);
    }
  }

  private static void CustomDnsWithEAP_GetHostEntryCompleted(object sender, CustomDnsEventArgs<IPHostEntry> e)
  {
    Console.WriteLine($"Worker Thread ID: {Thread.CurrentThread.ManagedThreadId}, Result: {e.Result.HostName}");

    _throttle.Release();
  }
}

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);
        });
    }
}
Mono C# compiler version 4.0.4.0