后台工作人员多次运行doWork

时间:2014-11-19 12:01:44

标签: 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();
        }
    }

这是我的Click事件。

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

RunWorkComplete代码

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

有人能看到这里的主要问题吗?

2 个答案:

答案 0 :(得分:3)

我针对@galenus建议的样本。我和后台工作者有相同的情况,点击多次触发dow工作按钮直到我用按钮初始化工人。 代码块:

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

    }
}

答案 1 :(得分:0)

尽管这可能可行,但是您可以在MainWindow的构造函数中声明并初始化worker,之后还可以附加事件。这样,在Button_Click方法中,您只需要运行worker即可。无需每次单击按钮时都需要创建新工作线程并附加这些事件,您可以节省一些时间;)