0

我正在将文件读入内存,然后尝试为每一行进行 Web 服务调用。我正在尝试做的事情是这样的:1.读入文件2.创建新线程->为第1行执行Web服务调用3.创建新线程->为第2行执行Web服务调用4.更新“状态" 带有服务调用 1 或 2 的结果的框,哪个先返回

所以换句话说,我正在尝试为文件中的每一行进行多个 Web 服务调用,并在它们返回时更新状态框。我不想打一个电话,然后在第一个电话返回后开始第二个电话。

编辑:忘了提到在调试时,我注意到只进行了一次调用,因为在readFileBackgroundWorker.IsBusy通过trueforeach 循环时,但是如果我删除它,我会收到一个错误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;
    }
4

3 回答 3

1

您的后台工作人员只运行一次的原因是您RunWorkerAsync()第二次调用时它很忙。您应该为循环的每次迭代创建一个后台工作程序。

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;
}

请在取消 BackGroundWorker DoWork上查看这个 SO 问题

于 2013-04-04T12:34:38.960 回答
0

您应该在工作人员完成工作之前尝试使用多线程作为 foreach 循环完成。

于 2013-04-04T12:29:21.713 回答
0

您可以使用类似的方法启动多个后台工作人员以并行执行您的 Web 服务调用

foreach (var client in clientList)
    {
        BackgroundWorker worker = new BackgroundWorker();
        worker.DoWork += new DoWorkEventHandler(worker_DoWork);
        worker.RunWorkerAsync(i);
    }
于 2013-04-04T12:35:37.507 回答