Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 后台工作进程报告未启动_C#_Backgroundworker - Fatal编程技术网

C# 后台工作进程报告未启动

C# 后台工作进程报告未启动,c#,backgroundworker,C#,Backgroundworker,我第一次设置了后台工作人员。它主要在代码运行和我的停止/取消按钮工作时工作。然而,我也试图报告进度以更新进度条,但我根本无法启动它 我从运行此代码的按钮单击开始代码: backgroundWorker1.WorkerSupportsCancellation = true; backgroundWorker1.WorkerReportsProgress = true; backgroundWorker1.RunWorkerAsync();//this invokes the DoWork even

我第一次设置了后台工作人员。它主要在代码运行和我的停止/取消按钮工作时工作。然而,我也试图报告进度以更新进度条,但我根本无法启动它

我从运行此代码的按钮单击开始代码:

backgroundWorker1.WorkerSupportsCancellation = true;
backgroundWorker1.WorkerReportsProgress = true;
backgroundWorker1.RunWorkerAsync();//this invokes the DoWork event 
我的工作方法:

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

            int j = 0;// Count cumulative imported files
            int countDupFiles = 0;// Count number of previously imported csv files
            int countImportedFiles = 0;// Count imported files


            foreach (string folderPath in csvDirList)
            {
                string[] csvFileNames = Directory.GetFiles(@folderPath, "*.csv");
                frmImportCsvData.replaceAll(csvFileNames, folderPath + "\\", "");

                for (int i = 0; i < csvFileNames.Length; i++, j++)
                {
                    string csvFilePath = folderPath + "\\" + csvFileNames[i];

                    if ((worker.CancellationPending == true))
                    {
                        e.Cancel = true;
                        break;
                    }
                    else
                    {
                        if (dataLayer.ImportCsvDataBkgrnd(this, csvFilePath, compIdValue, csvFileCount, i))//new method processes subdirectories if tick box selected
                        {
                            countImportedFiles = countImportedFiles + 1;
                        }
                        else
                        {
                            countDupFiles = countDupFiles + 1;
                        }

                        System.Threading.Thread.Sleep(500);

                    }

                    worker.ReportProgress(j);//tried using worker and backgroundWorker1 but neither works
                    backgroundWorker1.ReportProgress(j);

                    //string proj = j.ToString();
                    //MessageBox.Show(proj);//Displays incrementing j as expected when not commented out
                }
            }
            if (countImportedFiles > 0)
                MessageBox.Show(countImportedFiles + " files were imported.");
            if (countDupFiles > 0)
                MessageBox.Show(countDupFiles + " files were not imported. Matches all ready in Database.");
        }
最后,我希望ProgressChanged事件触发此方法以更新我的进度条:

public void importProgressBar(int i)
{
    progressTableLayoutPanel.Visible = true;//display progress bar

    int percProgress = 100 * (i + 1) / csvFileCount;

    if (percProgress <= 99)// Required to prevent values above 100 that crash the code
        progressBar.Value = percProgress + 1;//hack that makes the progress bar update when progress value decreases
    progressBar.Value = percProgress;
    percProgressLabel.Text = percProgress.ToString();

    progressTableLayoutPanel.Update();//Required to display all progress bar table contents
    //Thread.Sleep(200);

    if (percProgress >= 100)
    {
        Thread.Sleep(200);
        progressTableLayoutPanel.Visible = false;
    }
}
my ProgressChanged事件中的消息框从未显示,并且我的进度条从未设置为可见。你知道问题出在哪里吗?

检查这个例子:

    BackgroundWorker bgw = new BackgroundWorker();       
    public Form1()
    {
        InitializeComponent();
        label1.Text = "";
        label2.Text = "";
    }

   private void button1_Click_1(object sender, EventArgs e)
{
    if (bgw == null)
    {
        bgw = new BackgroundWorker();
        bgw.DoWork += new DoWorkEventHandler(bgw_DoWork);
        bgw.ProgressChanged += new ProgressChangedEventHandler(bgw_ProgressChanged);
        bgw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgw_RunWorkerCompleted);
    }
    bgw.WorkerReportsProgress = true;
    bgw.WorkerSupportsCancellation = true;
    bgw.RunWorkerAsync();
}

    void bgw_DoWork(object sender, DoWorkEventArgs e)
    {
        int total = 57; //some number (this is your variable to change)!!

        for (int i = 0; i <= total; i++) //some number (total)
        {
            System.Threading.Thread.Sleep(100);
            int percents = (i * 100) / total;
            bgw.ReportProgress(percents, i);
            //2 arguments:
            //1. procenteges (from 0 t0 100) - i do a calcumation 
            //2. some current value!
        }
    }

    void bgw_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        progressBar1.Value = e.ProgressPercentage;
        label1.Text = String.Format("Progress: {0} %", e.ProgressPercentage);
        label2.Text = String.Format("Total items transfered: {0}", e.UserState);
    }

    void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
         //do the code when bgv completes its work
    }
}
BackgroundWorker bgw=新的BackgroundWorker();
公共表格1()
{
初始化组件();
标签1.Text=“”;
标签2.Text=“”;
}
私有无效按钮1\u单击\u 1(对象发送者,事件参数e)
{
如果(bgw==null)
{
bgw=新的BackgroundWorker();
bgw.DoWork+=新DoWorkEventHandler(bgw_DoWork);
bgw.ProgressChanged+=新的ProgressChangedEventHandler(bgw\U ProgressChanged);
bgw.RunWorkerCompleted+=新的RunWorkerCompletedEventHandler(bgw\u RunWorkerCompleted);
}
bgw.WorkerReportsProgress=true;
bgw.workersupport扫描单元=真;
bgw.RunWorkerAsync();
}
无效bgw_DoWork(对象发送方,DoWorkEventArgs e)
{
int total=57;//一些数字(这是要更改的变量)!!

对于(int i=0;我使用的是工具箱中的backgroundworker组件。但是像您的示例中那样手动设置它,我让它全部工作。ThanksI发现了一个bug。如果我多次运行导入过程,会创建一个额外的后台工作程序,因此在第二次运行时,所有代码都运行了两次,第三次运行了三次,等等。如果(bgw=null)语句阻止创建多个bgw。完成,tnx以获取建议!正在触发进度更改,但我在事件期间对标签所做的任何更改都不会反映在UI中。我只是简单地将“处理”一词放在UI中。但在执行期间,对于那些需要在执行期间更新UI的用户,这不会在UI中更改。请检查控制。在此处调用
private void stopImportButton_Click(object sender, EventArgs e)
        {
             backgroundWorker1.CancelAsync();
        }
    BackgroundWorker bgw = new BackgroundWorker();       
    public Form1()
    {
        InitializeComponent();
        label1.Text = "";
        label2.Text = "";
    }

   private void button1_Click_1(object sender, EventArgs e)
{
    if (bgw == null)
    {
        bgw = new BackgroundWorker();
        bgw.DoWork += new DoWorkEventHandler(bgw_DoWork);
        bgw.ProgressChanged += new ProgressChangedEventHandler(bgw_ProgressChanged);
        bgw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgw_RunWorkerCompleted);
    }
    bgw.WorkerReportsProgress = true;
    bgw.WorkerSupportsCancellation = true;
    bgw.RunWorkerAsync();
}

    void bgw_DoWork(object sender, DoWorkEventArgs e)
    {
        int total = 57; //some number (this is your variable to change)!!

        for (int i = 0; i <= total; i++) //some number (total)
        {
            System.Threading.Thread.Sleep(100);
            int percents = (i * 100) / total;
            bgw.ReportProgress(percents, i);
            //2 arguments:
            //1. procenteges (from 0 t0 100) - i do a calcumation 
            //2. some current value!
        }
    }

    void bgw_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        progressBar1.Value = e.ProgressPercentage;
        label1.Text = String.Format("Progress: {0} %", e.ProgressPercentage);
        label2.Text = String.Format("Total items transfered: {0}", e.UserState);
    }

    void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
         //do the code when bgv completes its work
    }
}