单个 Windows Form C# 中的多个 BackGroundWorker 组件

Multiple BackGroundWorker components in a single Windows Form C#

在讨论我的问题之前,我想了解一下我的 stage.I 我是 C# 编程的新手。这是我第一次做这件事。所以我对 C# 的了解非常少。

我正在使用 C# 中的 Windows 表单开发我的应用程序。在某个时刻,我应该同时进行 运行 5 次操作。所以我在C#的工具箱中尝试了BackGroundWorker组件。

但是使用它,我只能处理 5 个操作中的一个。我尝试使用工具箱中的 5 个 BackGroundWorker 组件并单独定义 DoWork 函数。

但是当我调用 RunWorkerAsync() 函数时,它抛出了一个错误“backgroundworker is currently busy and cannot run multiple tasks concurrently”。

我不知道是否可以在我的 program.Creating 数组中使用多个 BackGroundWorker 对我没有帮助。因为我在 BackGroundWorker 组件的 DoWork 函数中有一个 运行 的无限循环。如果有任何其他方法可以同时 运行 5 个操作,请帮助我了解一下。提前致谢。

这是我的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Runtime.InteropServices;


namespace myapp5
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            bw.WorkerSupportsCancellation = true;
            bw.WorkerReportsProgress = true;

            bw.DoWork += new DoWorkEventHandler(bw_DoWork);

            bw1.WorkerSupportsCancellation = true;
            bw1.WorkerReportsProgress = true;

            bw1.DoWork += new DoWorkEventHandler(bw1_DoWork);
        }

        BackgroundWorker bw = new BackgroundWorker();
        BackgroundWorker bw1 = new BackgroundWorker();
        private void bw_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;

            if ((worker.CancellationPending == true))
            {
                e.Cancel = true;
            }
            else
            {
                try
                {
                    videostream();
                }
                catch (Exception exc)
                {
                    MessageBox.Show(exc.Message);
                }
            }
        }
        private void bw1_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;

            if ((worker.CancellationPending == true))
            {
                e.Cancel = true;
            }
            else
            {
                try
                {
                    videostream();
                }
                catch (Exception exc)
                {
                    MessageBox.Show(exc.Message);
                }
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {

            if (bw.IsBusy != true)
            {
                bw.RunWorkerAsync();
            }
            else
            {
                MessageBox.Show("Background Worker is busy");
            }
        }
        private void button3_Click(object sender, EventArgs e)
        {
            if (bw.WorkerSupportsCancellation == true)
            {
                bw.CancelAsync();
                bw.Dispose();
                MessageBox.Show("Background Closed");
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (bw1.IsBusy != true)
            {
                bw1.RunWorkerAsync();
            }
            else
            {
                MessageBox.Show("Background Worker is busy");
            }
        }
        private void button5_Click(object sender, EventArgs e)
        {
            if (bw1.WorkerSupportsCancellation == true)
            {
                bw1.CancelAsync();
                bw1.Dispose();
                MessageBox.Show("Background Closed");
            }
        }
    }
}

如果您想并行执行 5 个不同的功能,则需要 5 个后台工作线程。一个 Backgroupwork 线程不足以并行 运行 5 个不同的函数。

您也可以为此使用 TPL(taksparallel) 库而不是 backgroundworker。

您应该尝试标称用途的线程。

private object threadUnsafeObject;
    private object locker = new object();

    private void RunMulitpleThread()
    {
        object pass_your_parameter_here = "";
        for (int iCount = 0; iCount < 5; iCount++)
        {
            System.Threading.Thread thread = new System.Threading.Thread(DoWork);
            thread.Start(pass_your_parameter_here);

        }
    }

    private void DoWork(object parameters)
    {
        lock (locker)
        {
            threadUnsafeObject = parameters;
        }
    }

我认为您的程序存在逻辑错误,因此同一个 BackgroundWorker 实例被五次用于调用 RunWorkerAsync()。查看此代码。

UPD

您的 DoWork 处理程序必须如下所示:

private void bw_DoWork(object sender, DoWorkEventArgs e)
{
    BackgroundWorker worker = sender as BackgroundWorker;

    while (true)
    {
        if ((worker.CancellationPending == true))
        {
            e.Cancel = true;
            break;
        }
        else
        {
            // perform work partially with short time!
            // and break the while loop at the end
        }
    }
}

UPD-2

不要对工人使用 Dispose()。

如果您使用的是 .net 4.0 或更高版本,则应使用 Tasks。这就是 .NET 框架的发展方向。我已经设置了两个小提琴供您检查。 如果您使用的是 .NET 4.0:https://dotnetfiddle.net/GUxD2j。 如果您使用的是 .NET 4.5:https://dotnetfiddle.net/NirDuU。此示例展示了 .NET 框架中引入的新等待/异步模式的使用