I have an application where I have many “Searches” running at the same time (the searches take 1 – 10 sec to complete, depending on how many results the are available) The problem is that the delay when searching keeps getting bigger ( I Think because 25 Max threads) Im using Backgroundworker Class Atm. So I looked up a few other implementations:
Simple Examples :
static void Main()
{
for (int i = 0; i < 500; i++)
{
try
{
new Thread(new ParameterizedThreadStart(doWork)).Start(i);
}
catch { }
}
Console.ReadLine();
}
static void doWork(object i)
{
Console.WriteLine(i + ": started");
Thread.Sleep(1000);
Console.WriteLine(i + " done");
Thread.CurrentThread.Abort();
}
But I get exceptions that I abort the threads (wich worries me)
So I tried whith the threadpool :
static void Main()
{
for (int i = 0; i < 500; i++)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(doWork), i);
}
Console.ReadLine();
}
static void doWork(object i)
{
Console.WriteLine(i + ": started");
Thread.Sleep(1000);
Console.WriteLine(i + " done");
}
But this goes verry slow…
I’m still looking for the best implementation, Can anyone help me ?
EDIT: The DoWork Method Makes a Network Connection (and wait for it to complete) This is With An API so I can’t do async
Thread pool tries to minimize number of threads created and instead of creating new threads for queued tasks it can wait for other threads in the pool to be freed. It does that for a reason – too many threads can hamper performance. But you can override minimum number of threads to be created before this throttling happens.
Here is your original code with correction to make it work fast: