“线程正在中止” 显示对话框时出现异常
在我的应用程序中,我有一个线程显示一段时间的“请等待”对话框窗口,有时时间非常短,并且在绘制 UI 时出现一些故障(我猜)。
我收到异常“线程被中止”并且完全不知道如何摆脱它。 我的意思是以某种方式捕获该异常,或者以其他方式向用户隐藏它。 此异常与我的应用程序的其余部分无关,并且该错误以任何方式都不会影响它。 随机出现,并且很难在通话中重新创建。
我尝试以各种方式通过使用对话框窗口启动和停止线程的代码来捕获该异常,但似乎错误显然是在我新创建的线程中显示窗口的其他线程旁边。
这是一个代码示例,是静态类的一部分,其中包含有用的东西,当然我并不是说这是解决这种“繁忙”情况的好方法,但我想解决这个问题。 线程.睡眠(500); 或其他 try/catch 改进并不能帮助我避免该线程异常。
public static bool alreadyBusy = false;
public static BusyIndicator bi = new BusyIndicator("");
public static Thread backgroundOpertionThread;
public static void showBusy(bool isBusy, System.Windows.Forms.Form hostform, string message)
{
Common.busyMessage = message;
if (isBusy)
{
Common.alreadyBusy = true;
backgroundOpertionThread = new Thread(new ThreadStart(showBusy));
Thread.Sleep(500);
if (hostform != null)
{
hostform.Enabled = false;
hostform.SuspendLayout();
}
backgroundOpertionThread.Start();
}
else
{
backgroundOpertionThread.Abort();
Thread.Sleep(500);
Common.alreadyBusy = false;
if (hostform != null)
{
hostform.Enabled = true;
hostform.ResumeLayout();
}
}
}
public static void showBusy()
{
BusyIndicator bir = new BusyIndicator(Common.busyMessage);
bir.ShowDialog();
}
有任何想法吗?
In my app I've got a thread which displays for some time "please wait" dialog window, sometimes it is a very tiny amout of time and there is some glitch while drawing UI (I guess).
I get the exception "Thread was being aborted" and completly have no idea how get rid of it. I mean Catch that exception in some way, or in some other way hide it from user. This exception has got nothing to do with rest of my app and that error in any way doesn't affect it. Appears randomly and it is hard to recreate on a call.
I tried in various ways to catch that exception by side of code which starts and stops thread with dialog window but it seems that error apparently is by side some other thread which dispalys window in my newly created thread.
Here is a code sample, part of static class with useful stuff, of course I don't say that is good way to solve this kind of "busy" situation but I want to solve this problem. Thread.Sleep(500); or other try/catch improvments doesn't help me to avoid that thread exception.
public static bool alreadyBusy = false;
public static BusyIndicator bi = new BusyIndicator("");
public static Thread backgroundOpertionThread;
public static void showBusy(bool isBusy, System.Windows.Forms.Form hostform, string message)
{
Common.busyMessage = message;
if (isBusy)
{
Common.alreadyBusy = true;
backgroundOpertionThread = new Thread(new ThreadStart(showBusy));
Thread.Sleep(500);
if (hostform != null)
{
hostform.Enabled = false;
hostform.SuspendLayout();
}
backgroundOpertionThread.Start();
}
else
{
backgroundOpertionThread.Abort();
Thread.Sleep(500);
Common.alreadyBusy = false;
if (hostform != null)
{
hostform.Enabled = true;
hostform.ResumeLayout();
}
}
}
public static void showBusy()
{
BusyIndicator bir = new BusyIndicator(Common.busyMessage);
bir.ShowDialog();
}
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不要不要使用Thread.Abort。 此方法保留用于 .NET 运行时需要强制终止线程以卸载程序时。
仅当您要卸载 AppDomain 并希望首先摆脱其中运行的线程时,您才能“安全”地使用此选项。
要摆脱线程,请以协作模式编写它。 这意味着线程应该定期检查某种标志,如果设置了该标志,则正常退出线程方法。 要“杀死”线程,只需设置标志并等待线程退出即可。
您可以为此标志使用事件对象或简单的布尔变量。
但不要使用Thread.Abort。
Do not use Thread.Abort. This method is reserved for when the .NET runtime needs to forcibly kill threads in order to unload your program.
You can only "safely" use this if you're about to unload an AppDomain and want to get rid of threads running in it first.
To get rid of a thread, write it in cooperative mode. This means that the thread should periodically check some kind of flag, and if the flag is set, exit normally out of the thread method. To "kill" the thread, simply set the flag and wait for the thread to exit.
You can use an Event-object or a simple boolean variable for this flag.
But do not use Thread.Abort.
使用 SafeThread 并将 ShouldReportThreadAbort 设置为 false 以使问题消失......
更好的解决方案是在调试器中使用 Debug >> 来运行它 异常>> 抛出检查所有异常类型,并找出中止线程的情况
编辑:感谢您发布代码示例,这使问题更容易看到。 不要调用 THREAD.ABORT
use SafeThread and set ShouldReportThreadAbort to false to make the problem go away...
a better solution would be to run this in the debugger with Debug >> Exceptions >> Thrown checked for all exception types, and figure out what is happening to abort your thread
EDIT: thanks for posting the code sample, that makes the problem much easier to see. DO NOT CALL THREAD.ABORT
同意史蒂文关于在抛出时打开所有异常的想法。 然后您将能够立即看到问题。
需要记住的一件事很重要,如果您的 Visual Studio 设置为常规设置,而不是“Visual C# Developer”设置,则您将不会有调试菜单选项。 如果我使用新机器或使用新配置文件,仍然会引起我的注意......
Agreed with Steven's idea about turning on all exceptions when thrown. Then you'll be able to immediately see the problem.
One thing that's important to remember, you won't have the debug menu option if your visual studio settings are on general, as opposed to the "Visual C# Developer" setting. Still catches me if I'm on a new machine or using a new profile...
您可以尝试
在之后
调用这会给后台线程 500 毫秒的时间来完成主线程有机会调用
Edit 之前需要做的事情:但我同意最好的解决方案是根本不使用 Thread.Abort()
You can try calling
after
This would give the background thread 500 ms to do what it needs to do before the main thread gets an opportunity to call
Edit: But I agree that the best solution would be not to use Thread.Abort() at all