我正在将文件读入内存,然后尝试为每一行进行 Web 服务调用。我正在尝试做的事情是这样的:1.读入文件2.创建新线程->为第1行执行Web服务调用3.创建新线程->为第2行执行Web服务调用4.更新“状态" 带有服务调用 1 或 2 的结果的框,哪个先返回
所以换句话说,我正在尝试为文件中的每一行进行多个 Web 服务调用,并在它们返回时更新状态框。我不想打一个电话,然后在第一个电话返回后开始第二个电话。
编辑:忘了提到在调试时,我注意到只进行了一次调用,因为在readFileBackgroundWorker.IsBusy
通过true
foreach 循环时,但是如果我删除它,我会收到一个错误This BackgroundWorker is currently busy and cannot run multiple tasks concurrently.
读取文件:
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;
}