I am currently having an issue with BackgroundWorker
running on Windows Server 2003. I have a window application that need to run more than 50 threads
.
The code I wrote use BackgroundWorker
(BW) as a Thread wrapper to update data onto a window form. The issue is that the code are able to run more than 50 BWs on my XP machine, but stop at 50 when running on Windows 2003 server.
At first I though there are some kind of limit on the number of thread per app can run. Googling the issue shows that is not the case. I wrote the following code to confirm that.
static int count = 0;
static void Main(string[] args)
{
int max = 55; // default value
if (args.Length > 0)
// use command line parameter if provided
max = Convert.ToInt32(args[0]);
List<Thread> threadList = new List<Thread>();
try
{
while (count < max)
{
Thread newThread = new Thread(
new ParameterizedThreadStart(DummyCall), 1024);
newThread.Start(count);
threadList.Add(newThread);
count++;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
Console.ReadLine();
}
static void DummyCall(object obj)
{
Console.WriteLine(obj.ToString());
Thread.Sleep(1000000000);
}
The result show as expected. I can see a list of number from 0 to 54 on both my XP machine and the 2003 server.
However, when I try using the BW instead, my XP machine run to 54 and the 2003 server run to 49 (50 BWs). Here is the code.
static int count = 0;
static void Main(string[] args)
{
int max = 55; // default value
if (args.Length > 0)
// use command line parameter if provided
max = Convert.ToInt32(args[0]);
List<BackgroundWorker> list = new List<BackgroundWorker>();
try
{
while (count < max)
{
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += new DoWorkEventHandler(worker_DoWork);
worker.RunWorkerAsync(count);
list.Add(worker);
count++;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
Console.ReadLine();
}
static void worker_DoWork(object sender, DoWorkEventArgs e)
{
Console.WriteLine(e.Argument.ToString());
Thread.Sleep(1000000000);
}
So, the question is that why there is a limit on number of BW instances can run on the 2003 server but not XP? Is there anyway I can increase the number of BW instances on 2003 server? If yes, how can I do that?