应用程序进入exitinstance函数并退出线程 [英] Application goes into the exitinstance function and exit the thread

查看:120
本文介绍了应用程序进入exitinstance函数并退出线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我有一个在单个线程上运行的应用程序并执行一些计算操作。我的问题是什么会导致应用程序自动退出线程并转到exitinstance()函数并退出,而不给我一个退出线程的命令。关于为什么会发生这种情况的任何可能的建议?

Hello,

I have an application that runs on a single thread and carries out some calculation operation. My question is what would cause the application to quit the thread automatically and go to the exitinstance() function and exits out without me giving it a command to exit the thread. Any possible suggestions on why this is happening?

推荐答案

这是一个难题。当你退出应用程序时,所有其他线程应该退出它们的线程函数,否则它们将阻止你的应用程序进程终止。



当线程函数从不出现时出现问题返回,这是一个非常常见的情况。



通常的技术是以合作方式返回线程函数。换句话说,线程应该自己退出该函数。一种方法是让UI线程设置退出标志变量,并由其他线程定期检查。在更复杂的情况下,检查是在堆栈更深层的函数中完成的;那么展开堆栈的完美方法就是抛出一个特殊的退出异常(任何抛出的不同对象都没问题;你需要的只是所有其他异常情况);顺便说一句,所有异常通常都应该被抓住每个线程堆栈的最顶层堆栈框架。



我明白这个解决方案有多丑,但是这是通常的技术。轮询始终是性能损失和开发/支持的麻烦。但是其他技术被认为是危险的。



现在,有一种先进的安全线程中止技术由一位微软开发人员开发和发布。不幸的是,我目前没有参考,但我实现了它,并了解实现和功能的每个细节。很少有人知道这项工作,但这种方法本身最终成为了你不会使用的.NET。它使用Windows线程的抢先性质。我提到它是为了完整性,但现在不想解释它,因为:1)我不相信你会在实践中实现它,这是一个非常低级的方法; 2)该方法基于完全记录但不可移植的Windows API;换句话说,您需要为不同的CPU实现不同的实现; 3)这种方法的安全性常常引发激烈的火焰战争(即使在讨论更稳定的.NET线程流产时)我也不想进入;我个人确信这种方法非常安全可靠,但我同意即使使用它也需要开发人员使用它很好地理解技术,否则这种技术会引起麻烦;我支持这种技术的最终论点是,还有许多其他技术也需要深刻理解,否则很危险,但它们仍然不会引起火焰战争。



如果你很好奇,我会给你更多细节。




另见我过去的回答:< a href =http://www.codeproject.com/Answers/182968/Close-correcly-the-thread-inside-a-dll#answer1>正确关闭dll内的线程 [ ^ ] 。



-SA
This is one of the difficult problems. When you exit the application, all other threads should exit their thread functions, otherwise they will hold your application process from terminating.

The problem appears when the thread function "never returns", which is a very usual case.

The usual technique is returning the thread function in cooperative manner. In other words, the thread should exit this function by itself. One way to do it is having the "exit flag" variable set by the UI thread and checked by the other thread(s) periodically. In more complex cases, the check is done in the functions deeper on stack; then the perfect way to unwind the stack would be throwing a special "exit" exception (any distinct object thrown would be fine; all you need from it is being different from all other cases of exceptions); by the way, all exceptions are usually should be caught of the very top stack frame of the stack of each thread.

I do understand how ugly this solution is, but this is the usual technique. Polling is always a performance hit and a development/support hassle. But other techniques are considered dangerous.

Now, there is an advanced technique of safe thread abort developed and published by one Microsoft developer. Unfortunately, I don''t have a reference at this time, but I implemented it and know every detail of implementation and functionality. Very few people know that work, but the approach itself eventually made the way to .NET which you don''t use. It uses preemptive nature of the Windows threading. I mention it for completeness but don''t want to explain it right now, because: 1) I don''t believe you are going to implement it in practice, this is a very low-level method; 2) the approach is based on fully documented but not portable Windows API; in other words, you would need to have different implementations for different CPUs; 3) the safety of this approach used to cause furious flame wars (even in discussion of much more stable .NET thread abortion) I don''t want to get in; I am personally sure that the method is very safe and reliable, but I will agree that even the use of it requires very good understanding of the technique by the developer using it, otherwise the technique will cause the trouble; my ultimate argument in favor of the technique is that there are many other techniques which also require deep understanding and are dangerous otherwise, but still they don''t cause flame wars.

If you are curious, I''ll give you more detail.


See also my past answer: Close correcly the thread inside a dll[^].

—SA


这篇关于应用程序进入exitinstance函数并退出线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆