当我在一个漫长的过程中使用被调用的等待窗体的线程时,出现了错误。错误:线程在c#Windows应用程序中被中止
“的类型 'System.Threading.ThreadAbortException' 未处理的异常出现在 System.Windows.Forms.dll中
其他信息:线程已被中止。”
有时我的代码运行良好,但有时会出现此错误。
class ProgressCLS
{
private static Thread th = new Thread(new ThreadStart(showProgressForm));
public void startProgress()
{
th = new Thread(new ThreadStart(showProgressForm));
th.Start();
}
private static void showProgressForm()
{
Waiting sForm = new Waiting();
sForm.ShowDialog();
}
public void stopProgress()
{
th.Abort();
th = null;
}
}
我得到了showProgressForm()
方法这个错误在sform.ShowDialog()
线
,我叫这个类的主要程序是这样的:
ProgressCLS PC = new ProgressCLS();
PC.startProgress();
TodayDate = txtDate.SelectedDateTime.ToString("yy-MM-dd");
ClearField();
CalculateMSG();
tabControl1.SelectedIndex = 1;
btnShowFolderLocal.Enabled = true;
btnShowFolderTop.Enabled = true;
btnShowDpsFailed.Enabled = true;
btnShowDpsFailed2.Enabled = true;
btnShowFolderTopic.Enabled = true;
ShowMSGButtonClicked = true;
PC.stopProgress();
什么想法?
这是因为你使用了Thread.Abort(),并且你有一个try/catch-em-all语句,你没有发布。不要捕获ThreadAbortException。 –
您不需要单独的线程来显示进度表单。用'Show'而不是'ShowDialog'显示*非模态*形式。如果要报告其他任务/线程的进度,请使用“进度”并订阅其事件 –
[C#窗体控件不会使用多线程更新]的可能重复(http://stackoverflow.com/questions/38588884/ c-sharp-form-controls-wont-update-with-multithreading) –