Reputation: 385
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?
Upvotes: 0
Views: 195
Reputation: 203826
Leave this up to the thread pool. It almost certainly knows better than you what the optimal number of threads are.
Fundamentally your CPU is only capable of performing so many operations at one time. Creating more than a certain number of threads adds a lot of overhead in which cores are constantly swapping out which thread should be currently executing, while adding no additional throughput. Once you get to that point adding threads would increase each actions responsiveness, and improve the speed at which it gets started, but at the cost of total throughput.
If you really want to have more threads than that, you'll need to not use the thread pool and instead create thread explicitly. But if you're going to do this, you should really be sure that you know what you're doing, and know why the thread pool's allocation algorithm isn't well suited for you.
Upvotes: 1