c# - 后台 worker 多次运行 doWork

标签 c# backgroundworker

private void button_uploadToPI_Click(object sender, RoutedEventArgs e)
    {


        this.newFilePath = this.textbox_input_filePath.Text;
        this.label_status.Content = "";
        if (bgWorker.IsBusy != true)
        {


            bgWorker.RunWorkerAsync();
        }
    }

这是我的点击事件。

public MainWindow()
    {

        InitializeComponent();

        this.progressBar.Minimum = 0;
        this.progressBar.Maximum = 100;

        this.bgWorker = new BackgroundWorker();

        this.bgWorker.WorkerReportsProgress = true;
        this.bgWorker.WorkerSupportsCancellation = true;
        reader.IsOpen = false;

        this.bgWorker.DoWork += bgWorker_DoWork;
        this.bgWorker.ProgressChanged += bgWorker_ProgressChanged;
        this.bgWorker.RunWorkerCompleted += bgWorker_RunWorkerCompleted;
    }

主窗口方法。

public partial class MainWindow : Window
{

    private BackgroundWorker bgWorker;

bgWorker 在这里初始化。

void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {

        this.progressBar.Value = 0;
        this.label_progressPercentage.Content = "0%";
        reportTextBox.ScrollToEnd();
    }

运行工作完成代码

主要问题是,如果我运行应用程序一次,它会按预期工作。如果我再次单击,它将执行该方法两次,如果我再次单击它,它将执行该方法 3 次。每次新点击都会增加。

谁能看到这里的主要问题?

最佳答案

我根据@galenus 的建议处理的示例。我和后台工作人员有同样的情况,点击工作按钮多次触发工作,直到我用按钮初始化工作人员。 代码块:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.ComponentModel;
using System.Data;

namespace MouseClick
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {



        public MainWindow()
        {
            InitializeComponent();
        }

        public BackgroundWorker worker = new BackgroundWorker();

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //e.Handled = true; // Din't do anything 
            worker = new BackgroundWorker(); //stopped the dowork being executed multiple times when button pressed again in the same session
            worker.WorkerReportsProgress = true;
            worker.WorkerSupportsCancellation = true;
            worker.DoWork += worker_DoWork;
            worker.ProgressChanged += worker_ProgressChanged;
            worker.RunWorkerCompleted += worker_RunWorkerCompleted;
            worker.RunWorkerAsync();

        }

        void worker_DoWork(object sender, DoWorkEventArgs e)
        {


            int tot = 1;

            MessageBox.Show(tot.ToString()); //if this message box appears multiple times, it means the dowork is executing multiple times

            DataTable dt = new DataTable();

            dt.Columns.Add("Running Number");

            for (int i = 0; i <= 100; i++)
            {
                if (worker.CancellationPending == true)
                {
                    //http://stackoverflow.com/questions/8300799/cancel-background-worker-exception-in-e-result
                    //  e.Cancel = true; //This does the trick
                    e.Result = 100;
                    return;
                }
                worker.ReportProgress(i);
                System.Threading.Thread.Sleep(1);
                dt.Rows.Add(i);
            }
            e.Result = dt;
        }

        #region "worker_ProgressChanged"
        void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {

        }
        #endregion

        #region "worker_RunWorkerCompleted"
        void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            myGrid.ItemsSource = ((DataTable)e.Result).DefaultView;
            worker.Dispose();

        }
        #endregion

    }
}

关于c# - 后台 worker 多次运行 doWork,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27016324/

相关文章:

c# - 创建实现内部接口(interface)的类型

C#,后台 worker

c# - 将BackgroundWorker转换为具有模态显示的Task.Run

c# - 如何在 Visual Studio 中使用键盘查看封闭的 block /元素

.net - 取消 e.result 中的后台工作异常

c# - 知道 ThreadPool 何时为空

c# - 无法获取ListView跨线程中的项目

c# - 如何使用 AutoMapper 将嵌套列表映射到另一个列表

c# - 使用 C# 的条件 JSON 序列化

c# - asp.net 身份中的用户名验证消息问题