BackgroundWorker被调用两次

本文关键字:两次 调用 BackgroundWorker | 更新日期: 2023-09-27 18:06:40

我有一个后台工作的问题,它被调用了两次,因此增加了我的长例程的执行时间,我手动创建了后台工作,所以没有机会在InitializeComponent()方法中初始化DoWork,任何帮助都是值得赞赏的。

下面是我的代码:
namespace formStudent2
{
    public partial class formStudent2 : Form
    {
        private BackgroundWorker backgroundWorker1 = new BackgroundWorker();
        private ProgressBar progressBar1 = new ProgressBar();
        private Label label1 = new Label();
        public formStudent2()
        {
            InitializeComponent();
        }
        ...
        private void btnExportCsv_Click(object sender, EventArgs e)
        {
            // Path output file CSV
            string pathFile = @"D:'DataTest'ListStudent.csv";
            int filesCount = 3;
            // Check if the backgroundWorker is already busy running the asynchronous operation
            if (!backgroundWorker1.IsBusy)
            {
                btnExportCsv.Enabled = false;
                // This method will start the execution asynchronously in the background
                backgroundWorker1.RunWorkerAsync();
            }
            else
            {
                label1.Text = "Busy processing, please wait...";
            }
            /**
             * Display dialog Progress Bar : Exporting data...
             */
            Form form = new Form();
            form.Text = "Exporting data...";
            form.ClientSize = new Size(376, 100);
            form.FormBorderStyle = FormBorderStyle.FixedSingle;
            form.ShowIcon = false;
            form.ControlBox = false;
            form.MaximizeBox = false;
            form.MinimizeBox = false;
            form.StartPosition = FormStartPosition.CenterScreen;
            label1.AutoSize = true;
            label1.Location = new System.Drawing.Point(17, 25);
            label1.Name = "lblPercent";
            label1.Size = new System.Drawing.Size(102, 15);
            label1.TabIndex = 1;
            label1.Text = "0% Completed";
            form.Controls.Add(label1);
            Button btnCancel = new Button();
            btnCancel.Location = new System.Drawing.Point(268, 19);
            btnCancel.Name = "btnCancel";
            btnCancel.Size = new System.Drawing.Size(99, 27);
            btnCancel.TabIndex = 3;
            btnCancel.Text = "Cancel";
            btnCancel.UseVisualStyleBackColor = true;
            btnCancel.Click += (senderq, eq) =>
            {
                if (backgroundWorker1.IsBusy)
                {
                    // Cancel the asynchronous operation id still in progress
                    backgroundWorker1.CancelAsync();
                }
                else
                {
                    form.Close();
                }
            };
            form.Controls.Add(btnCancel);

            progressBar1.Location = new System.Drawing.Point(17, 61);
            progressBar1.Name = "progressBar1";
            progressBar1.Size = new System.Drawing.Size(350, 27);
            form.Controls.Add(progressBar1);

            backgroundWorker1.WorkerReportsProgress = true;
            backgroundWorker1.WorkerSupportsCancellation = true;
            backgroundWorker1.DoWork += (senderq, eq) =>
            {
                for (int i = 0; i < filesCount; i++)
                {
                    /**
                     * Export to file CSV
                     */
                    ExportFileCsv(pathFile, listStudent);
                    int percentage = (i + 1) * 100 / filesCount;
                    backgroundWorker1.ReportProgress(percentage);
                    // Set cancellation to pending
                    if (backgroundWorker1.CancellationPending)
                    {
                        // Execute cancellation
                        eq.Cancel = true;
                        backgroundWorker1.ReportProgress(0);
                        return;
                    }
                }
            };
            backgroundWorker1.ProgressChanged += (senderq, eq) =>
            {
                // This is updated from doWork. Its where GUI components are update,
                // receives updates after 100ms
                progressBar1.Value = eq.ProgressPercentage;
                label1.Text = eq.ProgressPercentage.ToString() + "% Completed";
            };
            backgroundWorker1.RunWorkerCompleted += (senderq, eq) =>
            {
                // Called when the heavy operation in background is over.
                // Can also accept GUI components
                if (eq.Cancelled)
                {
                    label1.Text = "Processing cancelled.";
                }
                else if (eq.Error != null)
                {
                    label1.Text = eq.Error.Message;
                }
                else
                {
                    btnExportCsv.Enabled = true;
                    MessageBox.Show("Export Finished!");
                    form.Close();
                }
            };
            form.ShowDialog();
        }
    }
}

BackgroundWorker被调用两次

worker的运行状态不是静态的。它依赖于一个新的后台工作线程的实例。试着让它共享。

下面是一些伪代码(我是一个java家伙,从来没有用c#编程过)来做这件事:

public partial class formStudent {
   boolean workerRunningStatus = false;
    private void btnExportCsv_Click(object sender, EventArgs e)
    {
        // Path output file CSV
        string pathFile = @"D:'DataTest'ListStudent.csv";
        int filesCount = 3;
        // Check if the backgroundWorker is already busy running the asynchronous operation
        if (!workerRunning)
        {
            btnExportCsv.Enabled = false;
            // This method will start the execution asynchronously in the background
            backgroundWorker1.RunWorkerAsync();
        }
        else
        {
            workerRunningStatus = true;
            //then exit
            return;
        }
        //Continue with the remaining logic
     }
}

这只是关于工作线程的一个实例在运行。或者看看如何在c#中使用单例模式。