BackgroundWorker RunWorkerAsync方法不会触发DoWork();

时间:2013-06-02 20:11:04

标签: c# backgroundworker

我一直在尝试让BackGroundWorker工作几个小时。在调用bgw.RunWorkerAsync()之后,我似乎无法找出为什么我的DoWork()事件处理程序没有被提升;

我已经减少了不重复的代码,所以阅读起来有点容易。

namespace FolderMonitor
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    using System.Windows.Forms;

    using FolderMonitor.Properties;

    public partial class MainForm : Form
    {
        private ExistingFileHandler exist = new ExistingFileHandler();
        private MonitoredFileHandler handler = new MonitoredFileHandler();
        private Monitor monitor;
        private BackgroundWorker bgw = new BackgroundWorker();

        public MainForm(Monitor monitor)
        {
            this.monitor = monitor;
            InitializeComponent();
            InitializeBackgroundWorker();
            txtFolderPath.Text = Settings.Default.monitoredFolder;
            txtNewFolderPath.Text = Settings.Default.destinationFolder;
            btnStop.Enabled = false;
        }

        private void InitializeBackgroundWorker()
        {
            bgw.DoWork += new DoWorkEventHandler(bgw_DoWork);
            bgw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgw_RunWorkerCompleted);
            bgw.ProgressChanged += new ProgressChangedEventHandler(bgw_ProgressChanged);
        }

        private void BtnStart_Click(object sender, EventArgs e)
        {
            btnStop.Enabled = true;
            btnStart.Enabled = false;
            exist.HandleExistingFiles(txtFolderPath.Text);
            listBoxFiles.Items.Clear();
            MonitoredFileHandler.MonitoredFolderPath = txtFolderPath.Text;
            MonitoredFileHandler.DestinationFolderPath = txtNewFolderPath.Text;
            this.bgw.RunWorkerAsync();
        }

        private void BtnStop_Click(object sender, EventArgs e)
        {
            this.bgw.CancelAsync();
            btnStart.Enabled = true;
            btnStop.Enabled = false;
        }

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

            if (!worker.IsBusy)
            {
                monitor.StartFolderMonitor(txtFolderPath.Text);
            }
        }

        private void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            btnStart.Enabled = true;
            btnStop.Enabled = false;
        }

        private void bgw_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            handler.MonitoredFiles.Add(txtNewFolderPath.Text);
        }
    }
}

3 个答案:

答案 0 :(得分:1)

BackgroundWorker.IsBusy在后​​台线程启动后将成立,因此在bgw_DoWork()内进行测试时始终如此。因此,永远不会调用monitor.StartFolderMonitor(txtFolderPath.Text)

你想要测试什么?我想你可以删除if (!worker.IsBusy)支票。

答案 1 :(得分:1)

除了前面提到的IsBusy问题之外,StartFolderMonitor以start开头而不是DoFolderMonitoring或类似的事实,它可能是非阻塞的,因此DoWork处理程序立即退出。

答案 2 :(得分:0)

  

致电

     

System.Windows.Forms.Application.DoEvents();

     

在bgw.RunWorkAsync();

之前