RunWorkerAsync()后,后台工作程序无法启动

时间:2014-05-15 14:25:54

标签: c# backgroundworker

C#的新手 - 我将我在网上发现的一个示例项目的代码部分拼凑在一起,这与我想要完成的事情很接近 - 并且它拒绝以同样的方式行事。到目前为止,我已经学到了很多东西并做了很多调整,但是这篇文章仍然无法实现,我在网上找不到任何相同的内容。在示例程序中,backgroundWorker.RunWorkerAsync();跳到程序的下一部分。在我的代码中,它没有赢。我在许多不同的地方放置了休息点,它总是停止并且似乎在RunWorkerAsync()处挂起。我认为问题的一部分是示例程序的原作者使用后台工作者的方式与我在网上看到的大多数示例没有对齐......但是当我运行它时,它在示例程序中有效在其自己的。我错过了什么?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;

namespace DIPUtil
{
public partial class DIPform : Form
{
    #region Fields
    private string outputDirectory;
    protected string findWhatString = "BEGIN";
    protected string replaceWithText = "FANOODLE";


    #endregion

    #region Background
    BackgroundWorker worker = new BackgroundWorker();

    /// <summary>
    /// Executes the main find and replace operation.
    /// </summary>
    /// <param name="worker">The BackgroundWorker object.</param>
    /// <returns>The number of files affected by the replace operation.</returns>
    private int DoFindReplace(BackgroundWorker worker)
    {
        //Initialize the affected count variable
        int filesAffectedCount = 0;

        //Initialize the counter
        int counter = 0;

        //Get all XML files in the directory
        string[] filesInDirectory = Directory.GetFiles(outputDirectory, "*.txt");

        //Initialize total file count
        int totalFiles = filesInDirectory.GetLength(0);

        //Analyze each file in the directory
        foreach (string file in filesInDirectory)
        {
            //Perform find and replace operation
            if (FindAndReplace(file))
            {
                //The file was changed so increment variable
                filesAffectedCount++;
            }

            //Increment the counter
            counter++;

            //Report progress
            worker.ReportProgress((int)((counter / totalFiles) * 100.00));
        }

        //Return the total number of files changed
        return filesAffectedCount;
    }
    #endregion

    #region FindAndReplace
    /// <summary>
    /// Performs the find and replace operation on a file.
    /// </summary>
    /// <param name="file">The path of the file to operate on.</param>
    /// <returns>A value indicating if the file has changed.</returns>
    private bool FindAndReplace(string file)
    {
        //holds the content of the file
        string content = string.Empty;

        //Create a new object to read a file
        using (StreamReader sr = new StreamReader(file))
        {
            //Read the file into the string variable.
            content = sr.ReadToEnd();
        }

        //Get search text
        string searchText = GetSearchText(findWhatString);

        //Look for a match
        if (Regex.IsMatch(content, searchText))
        {
            //Replace the text
            string newText = Regex.Replace(content, searchText, replaceWithText);

            //Create a new object to write a file
            using (StreamWriter sw = new StreamWriter(file))
            {
                //Write the updated file
                sw.Write(newText);
            }

            //A match was found and replaced
            return true;
        }

        //No match found and replaced
        return false;
    }
    #endregion

    #region Various
    /// <summary>
    /// Gets the text to find based on the selected options.
    /// </summary>
    /// <param name="textToFind">The text to find in the file.</param>
    /// <returns>The text to search for.</returns>
    private string GetSearchText(string textToFind)
    {
        //Copy the text to find into the search text variable
        //Make the text regex safe
        string searchText = Regex.Escape(findWhatString);

        return searchText;
    }
    /// <summary>
    /// Sets the properties of the controls prior to beginning the download.
    /// </summary>
    private void InitializeProcess()
    {
        //Get sources
        outputDirectory = txtDirectory.Text;

        //Set properties for controls affected when replacing
        statuslabel.Text = "Working...";
        progbar.Value = 0;
        progbar.Visible = true;
        btnprocess.Enabled = false;
        btncancel.Enabled = true;

        //Begin downloading files in background
        backgroundWorker.RunWorkerAsync();
    }

    /// <summary>
    /// Sets the properties of the controls after the download has completed.
    /// </summary>
    private void DeinitializeProcess()
    {
        //Set properties for controls affected when operating
        statuslabel.Text = "Ready";
        progbar.Visible = false;
        btnprocess.Enabled = true;
        btncancel.Enabled = false;
    }

    /// <summary>
    /// Displays the directory browser dialog.
    /// </summary>
    private void BrowseDirectory()
    {
        //Create a new folder browser object
        FolderBrowserDialog browser = new FolderBrowserDialog();

        //Show the dialog
        if (browser.ShowDialog(this) == DialogResult.OK)
        {
            //Set the selected path
            txtDirectory.Text = browser.SelectedPath;
        }
    }

    /// <summary>
    /// Validates that the user input is complete.
    /// </summary>
    /// <returns>A value indicating if the user input is complete.</returns>
    private bool InputIsValid()
    {
        //Set the error flag to false
        bool isError = false;

        //Clear all errors
        errorProvider.Clear();

        //Validate the directory name
        if (string.IsNullOrEmpty(txtDirectory.Text))
        {
            errorProvider.SetError(txtDirectory, "This is a required field.");
            isError = true;
        }
        else
        {
            //check to make sure the directory is valid
            if (Directory.Exists(txtDirectory.Text) == false)
            {
                errorProvider.SetError(txtDirectory, "The selected directory does not exist.");
                isError = true;
            }
        }

        //Return a value indicating if the input is valid
        if (isError)
            return false;
        else
            return true;
    }

    #endregion

    #region Events
    private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        //Create a work object and initialize
        BackgroundWorker worker = sender as BackgroundWorker;

        //Run the find and replace operation and store total files affected in the result property
        e.Result = (int)DoFindReplace(worker);
    }

    private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        //Update the prog bar
        progbar.Value = e.ProgressPercentage;
    }

    private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        //The background operation is done
        DeinitializeProcess();

        //Perform final operations
        if (e.Error != null)
            MessageBox.Show(this, e.Error.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        else if (e.Cancelled)
            MessageBox.Show(this, "The operation was ended by the user.", "Cancelled.", MessageBoxButtons.OK, MessageBoxIcon.Error);
        else
            MessageBox.Show(this, string.Format("{0} files were updated by the operation.", e.Result.ToString()), "Replace Complete", MessageBoxButtons.OK, MessageBoxIcon.Information);

    }
    public DIPform()
    {
        InitializeComponent();
    }

    private void DIPform_Load(object sender, EventArgs e)
    {

    }

    private void btnprocess_Click(object sender, EventArgs e)
    {
        //Verify input is ok
        if (InputIsValid())
            InitializeProcess();
    }

    private void btncancel_Click(object sender, EventArgs e)
    {

    }

    //private void linkbrowse_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
    // {
        //Select a directory to output files
    //    BrowseDirectory();
    //}
    private void linkbrowse_LinkClicked_1(object sender, LinkLabelLinkClickedEventArgs e)
    {
        //Select a directory to output files
        BrowseDirectory();
    }
    #endregion



}
}

2 个答案:

答案 0 :(得分:2)

当您致电RunWorkerAsync()时,会引发事件DoWork。发生这种情况时,将执行添加到此事件的方法:

backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) {/...}

检查您的设计师,看看它是否正确连接。

有关详细信息,请参阅此处:

BackGround Worker Class.

答案 1 :(得分:-3)

你期待什么?如果您通过设计器将后台工作人员放在表单上并连接事件处理程序,然后通过创建新的引用来手动分配对此的新引用(如您所做),变量内的引用值会发生变化,并且现在是变量中的另一个引用,事件侦查器之前附加到该引用。如果您使用设计器将其拖动到表单上,则无需创建新的BackroundWorker。如果您通过代码执行evrtything,而不是需要实例化,手动附加侦听器等等。两种方式都是如何处理它的部分排他方式,您可以混合它们,只有如果您有清晰的视图.. 。