I'm writing a little Proxy checker. I use this to manage the threads
que = new Queue<Proxy>(_settings.Proxies);
while (que.Count > 0)
{
lock (que)
{
while (running >= _settings.Threads) {}
Proxy cProxy = que.Dequeue();
CheckProxyAsync(cProxy);
Console.WriteLine(running);
}
}
I thought it would be a good idea to use async methods with callbacks to manage it!
this is the "CheckProxyAsync" function:
private void CheckProxyAsync(Proxy p)
{
InvokeDelegate.BeginInvoke(p, Callback, null);
}
this is my callback:
private void Callback(IAsyncResult ar)
{
var returnTuple = InvokeDelegate.EndInvoke(ar);
if (!returnTuple.Item1) //if failed
{
if (!_settings.Retry || returnTuple.Item2.Retries > _settings.RetryCount) //if no retry or retry count is reached
_settings.Proxies.Remove(returnTuple.Item2); //Dead proxy :(
else que.Enqueue(returnTuple.Item2); //try again
}
else //if success
{
Interlocked.Increment(ref filteredProxies);
Interlocked.Decrement(ref leftProxies);
if (returnTuple.Item2.ProxyAnonymity == Anonymity.L1)
Interlocked.Increment(ref l1Proxies);
else if (returnTuple.Item2.ProxyAnonymity == Anonymity.L2)
Interlocked.Increment(ref l2Proxies);
else if (returnTuple.Item2.ProxyAnonymity == Anonymity.L3)
Interlocked.Increment(ref l3Proxies);
}
OnUpdate();
}
As you can see in the manager function I print out the running thread count to the console. The count is getting updated at the start and at the end of the function "CheckProxy" to which the delegate "InvokeDelegate" belongs. but the maximum parallel running async methods are 8, but I want more! How can I increase the limit?