[英]If terminating a hung thread is a good idea, how do I do it safely?
My Delphi program relies heavily on Outlook automation. 我的Delphi程序在很大程度上依赖于Outlook自动化。 Outlook versions prior to 2007-SP2 tend to get stuck in memory due to badly written addins and badly written Outlook code.
2007-SP2之前的Outlook版本由于编写错误的插件和编写错误的Outlook代码而倾向于卡在内存中。
If Outlook is stuck, calling CreateOleObject('Outlook.Application') or GetActiveObject ... doesn't return and keeps my application hanging till Outlook.exe is closed in the task manager. 如果Outlook卡住,调用CreateOleObject('Outlook.Application')或GetActiveObject ...不会返回并保持我的应用程序挂起,直到任务管理器中的Outlook.exe关闭。
I've thought of a solution, but I'm unsure whether it's good practice or not. 我想到了一个解决方案,但我不确定这是否是好的做法。
I'd start Outlook with CreateOleObject in a separate thread, wait 10 seconds in my main thread and if Outlook hangs (CreateOleObject doesn't return), offer the user to kill the Outlook.exe process from my program. 我在一个单独的线程中使用CreateOleObject启动Outlook,在我的主线程中等待10秒,如果Outlook挂起(CreateOleObject没有返回),则让用户从我的程序中终止Outlook.exe进程。
But since I don't want to force the user to kill the Outlook.exe process, as an alternative I also need a way to kill the new thread in my program which keeps hanging now. 但是因为我不想强迫用户杀死Outlook.exe进程,作为替代方案,我还需要一种方法来杀死程序中的新线程,该线程现在一直挂起。
Windows has a TerminateThread function, but as you can see from the remarks, it's not generally a good idea to use it. Windows有一个TerminateThread函数,但从备注中可以看出,使用它通常不是一个好主意。 A safer approach would be to have a secondary application that interacts with Outlook, and you could then kill that without affecting your own application's stability.
更安全的方法是拥有一个与Outlook交互的辅助应用程序,然后您可以杀死它而不影响您自己的应用程序的稳定性。 TerminateProcess would work, but if you wanted to be a little friendlier to the system Dr. Dobbs has an article on a possibly safer approach using ExitProcess.
TerminateProcess可以工作,但是如果你想对系统更友好一点Dr.Dobbs有一篇关于使用ExitProcess可能更安全的方法的文章 。
If the hang is consistent and always either happens or doesn't happen, you can just call CreateOleObject in the app and exit, then call it again from your own. 如果挂起是一致的并且始终发生或不发生,您只需在应用程序中调用CreateOleObject并退出,然后再从您自己调用它。 If it's inconsistent the secondary application could be a more complete wrapper, and all the interactions would go through it.
如果它不一致,则辅助应用程序可能是更完整的包装器,并且所有交互都将通过它。
And additionally, you can use thread's Context's eip register. 此外,您还可以使用线程的Context的eip寄存器。
You can find a sample at; 你可以找到一个样本;
http://www.tugrulhelvaci.com/?p=568 http://www.tugrulhelvaci.com/?p=568
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.