我是线程的新手,我的 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();
}
}