ERROR : Thread was being aborted in c# Windows application
ERROR : Thread was being aborted in c# Windows application
在长时间的过程中使用线程调用等待表单时出现以下错误。
"An unhandled exception of type
'System.Threading.ThreadAbortException' occurred in
System.Windows.Forms.dll
Additional information: Thread was being aborted."
有时我的代码运行良好,但有时会出现此错误。
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;
}
}
我在 sform.ShowDialog()
行
的 showProgressForm()
方法上遇到了这个错误
我称之为 class 的主程序如下所示:
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();
有什么想法吗?
private static Thread th = new Thread(new ThreadStart(showProgressForm));
public void startProgress()
{
th = new Thread(new ThreadStart(showProgressForm));
th.Start();
}
没什么大不了的,但是为什么要实例化线程两次?不是很干净。
我认为只有你的 ctor 中的那个是强制性的,因为你在调用 stopProgress() 时设置了 th = null。
无论如何看看你的代码,记住线程是异步的,所以:
ProgressCLS PC = new ProgressCLS();
PC.startProgress();
它 运行 是你在专用线程中的进度表(异步,所以你的代码仍然是 运行ning)。
TodayDate = txtDate.SelectedDateTime.ToString("yy-MM-dd");
ClearField();
CalculateMSG();
...
您在主线程中执行了一系列进程(同步,您的进度表仍在后台 运行)。
PC.stopProgress();
无论进度表的状态如何,它都会被中止。正如您可能从 MSDN documentation 中遗漏的那样,它 "Raises a ThreadAbortException in the thread on which it is invoked"。因此,公平地说,您的代码 "sometimes work" 甚至很奇怪,因为如果它命中 th.Abort() 行,它应该会失败。
这里有一些提示:
- 通常我们运行 UI在主线程中形成,在后台处理
- 根据您当前的设计,如果您的任何进程(ClearField() 和 CalculateMSG())具有异步操作,您可能会遇到麻烦。
- 您很少需要明确地中止线程(仅当出现意外错误时)。进度完成后只需关闭表单,垃圾收集器即可完成剩下的工作。
全部发送。
我像这样更改了我的 Progress Class 代码:
//private static Thread th = new Thread(new ThreadStart(showProgressForm));
private static Thread th;
public void startProgress()
{
th = new Thread(new ThreadStart(showProgressForm));
th.Start();
}
成功了
在长时间的过程中使用线程调用等待表单时出现以下错误。
"An unhandled exception of type 'System.Threading.ThreadAbortException' occurred in System.Windows.Forms.dll
Additional information: Thread was being aborted."
有时我的代码运行良好,但有时会出现此错误。
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;
}
}
我在 sform.ShowDialog()
行
showProgressForm()
方法上遇到了这个错误
我称之为 class 的主程序如下所示:
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();
有什么想法吗?
private static Thread th = new Thread(new ThreadStart(showProgressForm));
public void startProgress()
{
th = new Thread(new ThreadStart(showProgressForm));
th.Start();
}
没什么大不了的,但是为什么要实例化线程两次?不是很干净。 我认为只有你的 ctor 中的那个是强制性的,因为你在调用 stopProgress() 时设置了 th = null。
无论如何看看你的代码,记住线程是异步的,所以:
ProgressCLS PC = new ProgressCLS();
PC.startProgress();
它 运行 是你在专用线程中的进度表(异步,所以你的代码仍然是 运行ning)。
TodayDate = txtDate.SelectedDateTime.ToString("yy-MM-dd");
ClearField();
CalculateMSG();
...
您在主线程中执行了一系列进程(同步,您的进度表仍在后台 运行)。
PC.stopProgress();
无论进度表的状态如何,它都会被中止。正如您可能从 MSDN documentation 中遗漏的那样,它 "Raises a ThreadAbortException in the thread on which it is invoked"。因此,公平地说,您的代码 "sometimes work" 甚至很奇怪,因为如果它命中 th.Abort() 行,它应该会失败。
这里有一些提示:
- 通常我们运行 UI在主线程中形成,在后台处理
- 根据您当前的设计,如果您的任何进程(ClearField() 和 CalculateMSG())具有异步操作,您可能会遇到麻烦。
- 您很少需要明确地中止线程(仅当出现意外错误时)。进度完成后只需关闭表单,垃圾收集器即可完成剩下的工作。
全部发送。 我像这样更改了我的 Progress Class 代码:
//private static Thread th = new Thread(new ThreadStart(showProgressForm));
private static Thread th;
public void startProgress()
{
th = new Thread(new ThreadStart(showProgressForm));
th.Start();
}
成功了