If terminating a hung thread is a good idea, how do I do it safely?

My Delphi program relies heavily on Outlook automation. Outlook versions prior to 2007-SP2 tend to get stuck in memory due to badly written addins and badly written Outlook code.

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.

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.

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.

  1. Is this good practice?
  2. How can I terminate a hanging thread in Delphi without leaking memory?

Answers


Windows has a TerminateThread function, but as you can see from the remarks, it's not generally a good idea to use it. 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. 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.

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. 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.

You can find a sample at;

http://www.tugrulhelvaci.com/?p=568


Need Your Help

Subsonic 3 ActiveRecord generation problem

c# asp.net-mvc subsonic subsonic3 subsonic-active-record

I´m newer user in stack overflow and my english i´snt so good i hope you understand my trouble.

release UIImagePickerControl in Objective C & IOS 5.1

objective-c ipad ios5

I got syntax error when I tried to release UIImagePickerControl after using use buttom