使用ThreadStart我的UI卡住了
本文关键字:UI ThreadStart 我的 使用 | 更新日期: 2023-09-27 18:10:11
我是新手我的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();
}
}
替换你的代码
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
-方法中,您正在做一些奇怪的事情
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,在标签控件上使用else if
!您必须对每个控件使用单独的if/else
。
if (this.progressBar1.InvokeRequired)
progressBar1.Invoke(new Action(delegate() { progressbar1.Value++; }));
else
progressbar1.Value++; //pseudo-code
你应该在WinForms中使用BackgroundWorker控件进行异步操作。有关其使用细节和示例,请参阅此MSDN文章。