Reputation: 5573
I'm reading a file to memory, and then trying to do a webservice call for each line. What I'm trying to do is something like: 1. Read in the file 2. Create new thread - > perform web service call for line 1 3. Create new thread - > perform web service call for line 2 4. Update "status" box with result from service call 1 or 2 which ever one gets back first
So in other words I'm trying to do multiple web service calls for each line in the file, and update the status box as they come back. I don't want to make one call, and then start the second call after the first call has returned.
EDIT: forgot to mention that while debugging, I noticed that only one call is made because the readFileBackgroundWorker.IsBusy
is true
when going through the foreach loop, but if I remove that then I get an error This BackgroundWorker is currently busy and cannot run multiple tasks concurrently.
Read file in:
private void uxReadFileButton_Click(object sender, EventArgs e)
{
uxFileStatusLabel.Text = String.Empty;
this.uxReadFileButton.Enabled = false;
this.uxCancelReadingFileButton.Enabled = true;
var clientList = ReadFile(uxFileNameBox.Text);
foreach (var client in clientList)
{
if (readFileBackgroundWorker.IsBusy != true)
{
readFileBackgroundWorker.RunWorkerAsync(client);
}
}
}
private void readFileBackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
var worker = (BackgroundWorker)sender;
ProcessClient((ClientObject)e.Argument, worker, e);
}
private void ProcessClient(ClientObject client, BackgroundWorker worker, DoWorkEventArgs e)
{
try
{
client.FileClientDischarge(SystemCode, UserName, Password);
int percent = (int)(Math.Ceiling(((double)(client.RecordNumber + 1) / 121) * 100));
worker.ReportProgress(percent, client);
}
catch (Exception ex)
{
worker.CancelAsync();
}
e.Result = client.RecordNumber;
}
Upvotes: 0
Views: 2502
Reputation: 1018
you can start multiple backgroundworkers to do your webservice calls in parallel by using something like
foreach (var client in clientList)
{
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += new DoWorkEventHandler(worker_DoWork);
worker.RunWorkerAsync(i);
}
Upvotes: 0
Reputation: 1447
The reason your background worker only runs once is that it's busy when you call RunWorkerAsync()
the second time. You should create a background worker for every iteration of the loop.
private void uxReadFileButton_Click(object sender, EventArgs e)
{
uxFileStatusLabel.Text = String.Empty;
this.uxReadFileButton.Enabled = false;
this.uxCancelReadingFileButton.Enabled = true;
var clientList = ReadFile(uxFileNameBox.Text);
BackGroundWorker bgw;
foreach (var client in clientList)
{
bgw = new BackgroundWorker();
bgw.DoWork += readFileBackgroundWorker_DoWork;
//bgw.RunWorkerCompleted +=
bgw.RunWorkerAsync(client);
}
}
private void readFileBackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
var worker = (BackgroundWorker)sender;
ProcessClient((ClientObject)e.Argument, worker, e);
}
private void ProcessClient(ClientObject client, BackgroundWorker worker, DoWorkEventArgs e)
{
try
{
client.FileClientDischarge(SystemCode, UserName, Password);
int percent = (int)(Math.Ceiling(((double)(client.RecordNumber + 1) / 121) * 100));
worker.ReportProgress(percent, client);
}
catch (Exception ex)
{
worker.CancelAsync();
}
e.Result = client.RecordNumber;
}
Please checkout this SO question on Cancelling a BackGroundWorker DoWork
Upvotes: 1
Reputation: 92
You should try using multi threading as the foreach loop finishes before your worker does the job.
Upvotes: 0