-1

我是线程的新手,我的 UI 在进程完成之前没有响应。任何人都告诉我这里有什么问题。

减少代码:

  static string replacedname = "", replacedwith;
            private void startProcess(string FPath, string RNo)
            {
                if (FPath != "" && RNo != "")
                {
                    ....

                    UnZip(FPath, Path.GetDirectoryName(FPath) + "\\" + replacedwith);

                    DirectoryInfo dir = new DirectoryInfo(Path.GetDirectoryName(FPath) + "\\" + replacedwith);
                    foreach (FileInfo File1 in dir.GetFiles("*.zip"))
                    {
                        UnZip(File1.FullName, Path.GetDirectoryName(File1.FullName));
                        File.Delete(File1.FullName);
                    }

                }

                replacedname = "";
                MessageBox.Show("Completed");

            }
            private void button1_Click(object sender, EventArgs e)
            {

                ThreadStart threadStart = delegate() { startProcess(textBox1.Text, textBox2.Text); };

                threadStart.BeginInvoke(null, null);


            }
            private void UnZip(string zipToExtract, string unzipDirectoryl) // Extract zip/lot file in same directory
            {

                if (this.progressBar1.InvokeRequired)
                {
                    progressBar1.Invoke(new Action(delegate() { UnZip(zipToExtract, unzipDirectoryl); }));

                }
                else if (this.label3.InvokeRequired)
                {
                    label3.Invoke(new Action(delegate() { UnZip(zipToExtract, unzipDirectoryl); }));
                }
                else
                {                        ....
                            progressBar1.Value += 1;
                            progressBar1.Refresh();
                            label3.Text = ((progressBar1.Value * 100) / (progressBar1.Maximum)).ToString();
                            label3.Refresh();
                        ...
                }
            }


            private void status(string msg, Color selcColor)
            {
                if (this.richTextBox1.InvokeRequired)
                {
                    richTextBox1.Invoke(new Action(delegate() { status(msg, selcColor); }));
                }
                else
                {
                    richTextBox1.SelectionStart = richTextBox1.Text.Length;
                    var oldcolor = richTextBox1.SelectionColor;

                    richTextBox1.SelectionColor = selcColor;
                    richTextBox1.AppendText(msg + "\n");
                    richTextBox1.SelectionColor = oldcolor;
                    richTextBox1.Refresh();
                    richTextBox1.ScrollToCaret();
                }
            }
4

2 回答 2

2

替换你的代码

 private void button1_Click(object sender, EventArgs e)
        {
            ThreadStart threadStart = delegate() { startProcess(textBox1.Text, textBox2.Text); };
            threadStart.BeginInvoke(null, null);
        }

 private void button1_Click(object sender, EventArgs e)
        {
            ThreadStart threadStart = delegate() { startProcess(textBox1.Text, textBox2.Text); };
            Thread thread = new Thread(threadStart);
            thread.Start();
        }

这将使用您分配的委托启动一个新线程。
有关 MSDN 参考,请参见此处

编辑

在您的UnZip-method 中,您正在做一些奇怪的事情

private void UnZip(string zipToExtract, string unzipDirectoryl){

            if (this.progressBar1.InvokeRequired)
            {
                progressBar1.Invoke(new Action(delegate() { UnZip(zipToExtract, unzipDirectoryl); }));

            }
            else if (this.label3.InvokeRequired) // why else if on another control???
            {
                label3.Invoke(new Action(delegate() { UnZip(zipToExtract, unzipDirectoryl); }));
            }
            else
            {                        ....
                        progressBar1.Value += 1;
                        progressBar1.Refresh(); //why use Refresh()?
                        label3.Text = ((progressBar1.Value * 100) / (progressBar1.Maximum)).ToString();
                        label3.Refresh();
                    ...
            }
        }

您不能调用progressbar1.InvokeRequired,如果它返回错误else if,请在标签控件上使用!您必须if/else对每个控件使用单独的。

if (this.progressBar1.InvokeRequired)
     progressBar1.Invoke(new Action(delegate() { progressbar1.Value++; }));
else 
     progressbar1.Value++; //pseudo-code 
于 2013-04-20T08:09:10.373 回答
0

您应该在 WinForms 中使用 BackgroundWorker 控件进行异步操作。有关其使用细节和示例,请参阅MSDN 文章。

于 2013-04-20T08:59:29.880 回答