T
thezim
I'm trying to figure out why the following doesn't work. It hang
waiting for a BackGroundWorker to become "un busy". With the 11 item
in the queue the first 10 get to the DoWork but never to RunCompleted.
If the items in the queue are equal to MaxWorker then everything works
okay.
I am goin insane. I knows its some hidden inherent way im doing
something that i have yet to see documented or maybe im just in over
my head.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using System.Diagnostics;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private BackgroundWorker[] Workers;
private int MaxWorkers = 10;
private void button1_Click(object sender, EventArgs e)
{
Queue<string> Clients = new Queue<string>();
Clients.Enqueue("TEST01");
Clients.Enqueue("TEST02");
Clients.Enqueue("TEST03");
Clients.Enqueue("TEST04");
Clients.Enqueue("TEST05");
Clients.Enqueue("TEST06");
Clients.Enqueue("TEST07");
Clients.Enqueue("TEST08");
Clients.Enqueue("TEST09");
Clients.Enqueue("TEST10");
Clients.Enqueue("TEST11");
Workers = new BackgroundWorker[MaxWorkers];
for (int i = 0; i < MaxWorkers; i++)
{
Workers = new BackgroundWorker();
Workers.WorkerSupportsCancellation = true;
Workers.DoWork += new
DoWorkEventHandler(bw_DoWork);
Workers.RunWorkerCompleted += new
RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
}
while (Clients.Count != 0)
{
for (int i = 0; i < 10; i++)
{
if (!Workers.IsBusy)
{
string hostname = Clients.Dequeue();
Debug.WriteLine("Starting " + hostname);
Workers.RunWorkerAsync(hostname);
}
}
}
}
private void bw_DoWork(object sender, DoWorkEventArgs e)
{
Debug.WriteLine("Working " + e.Argument.ToString());
e.Result = e.Argument.ToString();
}
private void bw_RunWorkerCompleted(object sender,
RunWorkerCompletedEventArgs e)
{
Debug.WriteLine("Completed " + e.Result);
}
}
}
waiting for a BackGroundWorker to become "un busy". With the 11 item
in the queue the first 10 get to the DoWork but never to RunCompleted.
If the items in the queue are equal to MaxWorker then everything works
okay.
I am goin insane. I knows its some hidden inherent way im doing
something that i have yet to see documented or maybe im just in over
my head.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using System.Diagnostics;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private BackgroundWorker[] Workers;
private int MaxWorkers = 10;
private void button1_Click(object sender, EventArgs e)
{
Queue<string> Clients = new Queue<string>();
Clients.Enqueue("TEST01");
Clients.Enqueue("TEST02");
Clients.Enqueue("TEST03");
Clients.Enqueue("TEST04");
Clients.Enqueue("TEST05");
Clients.Enqueue("TEST06");
Clients.Enqueue("TEST07");
Clients.Enqueue("TEST08");
Clients.Enqueue("TEST09");
Clients.Enqueue("TEST10");
Clients.Enqueue("TEST11");
Workers = new BackgroundWorker[MaxWorkers];
for (int i = 0; i < MaxWorkers; i++)
{
Workers = new BackgroundWorker();
Workers.WorkerSupportsCancellation = true;
Workers.DoWork += new
DoWorkEventHandler(bw_DoWork);
Workers.RunWorkerCompleted += new
RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
}
while (Clients.Count != 0)
{
for (int i = 0; i < 10; i++)
{
if (!Workers.IsBusy)
{
string hostname = Clients.Dequeue();
Debug.WriteLine("Starting " + hostname);
Workers.RunWorkerAsync(hostname);
}
}
}
}
private void bw_DoWork(object sender, DoWorkEventArgs e)
{
Debug.WriteLine("Working " + e.Argument.ToString());
e.Result = e.Argument.ToString();
}
private void bw_RunWorkerCompleted(object sender,
RunWorkerCompletedEventArgs e)
{
Debug.WriteLine("Completed " + e.Result);
}
}
}