Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.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#_.net_Winforms_Backgroundworker - Fatal编程技术网

C# 背景工作者“;原因“;冻结我的表格

C# 背景工作者“;原因“;冻结我的表格,c#,.net,winforms,backgroundworker,C#,.net,Winforms,Backgroundworker,在我的应用程序中,我想在后台创建数千个文件,但当我执行此操作时,它总是冻结我的表单(创建所有文件后,我可以再次使用我的表单)。如何在不挂起的情况下正常运行 private void button5_Click(object sender, EventArgs e) { BackgroundWorker bw = new BackgroundWorker(); bw.WorkerReportsProgress = true; bw.DoW

在我的应用程序中,我想在后台创建数千个文件,但当我执行此操作时,它总是冻结我的表单(创建所有文件后,我可以再次使用我的表单)。如何在不挂起的情况下正常运行

private void button5_Click(object sender, EventArgs e)
    {
        BackgroundWorker bw = new BackgroundWorker();

        bw.WorkerReportsProgress = true;

        bw.DoWork += new DoWorkEventHandler(
        delegate(object o, DoWorkEventArgs args)
        {
            BackgroundWorker b = o as BackgroundWorker;

            this.Invoke(new MethodInvoker(delegate
            {
                getValues();//load some text fields into strings

                while (counter < counted)
                {
                    text = richTextBox1.Text;
                    text = text.Replace("number", finalNumber);

                    //create copies
                    if (checkBox1.Checked == true)
                    {
                        while (createdCopies < copies)
                        {
                            createdCopies++;

                            File.WriteAllText(fileName, text);
                            overalCounter++;
                            b.ReportProgress(overalCounter);
                        }
                        counter++;
                        createdCopies = 0;
                    }
                    //dont create copies
                    else
                    {
                        File.WriteAllText(fileName, text);
                        counter++;
                        overalCounter++;
                        b.ReportProgress(overalCounter);
                    }
                    //info about number of files created
                    label6.Text = "created " + overalCounter.ToString() + " files";

                }
                label1.Text = "success";
            }));
    });

    if (bw.IsBusy != true)
    {
        bw.RunWorkerAsync();
    }

    bw.ProgressChanged += new ProgressChangedEventHandler(
    delegate(object o, ProgressChangedEventArgs args)
    {
        this.Text = string.Format("{0}% Completed", args.ProgressPercentage);
    });
    }
private void按钮5_单击(对象发送者,事件参数e)
{
BackgroundWorker bw=新的BackgroundWorker();
bw.WorkerReportsProgress=true;
bw.DoWork+=新DoWorkEventHandler(
委托(对象o、DoWorkEventArgs参数)
{
后台工作人员b=o作为后台工作人员;
this.Invoke(新方法调用器(委托
{
getValues();//将一些文本字段加载到字符串中
while(计数器<计数)
{
text=richTextBox1.text;
text=text.替换(“编号”,最终编号);
//创建副本
if(checkBox1.Checked==true)
{
while(创建的文件<副本)
{
createdCopies++;
WriteAllText(文件名,文本);
overalCounter++;
b、 报告进度(总计数);
}
计数器++;
createdCopies=0;
}
//不要复制
其他的
{
WriteAllText(文件名,文本);
计数器++;
overalCounter++;
b、 报告进度(总计数);
}
//有关创建的文件数的信息
label6.Text=“已创建”+overalCounter.ToString()+“文件”;
}
标签1.Text=“成功”;
}));
});
如果(bw.IsBusy!=真)
{
RunWorkerAsync();
}
bw.ProgressChanged+=新的ProgressChangedEventHandler(
委托(对象o,ProgressChangedEventArgs args)
{
this.Text=string.Format(“{0}%已完成”,args.ProgressPercentage);
});
}

在第一个学员中完成实际工作。第二个委托this.Invoke(…)在窗体(=主线程)的线程上执行,因此会阻止您的UI。

this.Invoke
在UI线程上运行内部代码,阻止任何UI更新。
由于您在
Invoke
方法中运行所有内容,因此所有内容都将在UI线程上运行

在修改UI控件的每个位置创建一个单独的
Invoke
,并将繁重的工作留在
Invoke
s之外