我是线程新手 我的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();
}
}
答案 0 :(得分: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参考,请参阅here。
修改强>
在你的UnZip
方法中,你正在做一些奇怪的事情
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
,如果返回false,请使用Label-control上的else if
!
您必须为每个控件使用单独的if/else
。
if (this.progressBar1.InvokeRequired)
progressBar1.Invoke(new Action(delegate() { progressbar1.Value++; }));
else
progressbar1.Value++; //pseudo-code
答案 1 :(得分:0)
您应该将BackgroundWorker Control用于WinForms中的异步操作。有关其用法详细信息和示例,请参阅this MSDN artical。