Jump to content
Mark Williams

Kill a thread ... dead

Recommended Posts

I am looking for a way to kill a hung thread, no questions asked!

 

I've tried to find an answer to this on the web, but the replies are usually "that's really bad dude, debug your code" which I would say is sound advice.

 

However, I am using OLE to open MS Powerpoint to convert to Powerpoint docs to pdf. This needs to be done in (sometimes large) batches along with many other conversion routines for other types of files.

 

The problem is that if you try and open a password protected Powerpoint the process simply hangs, nothing you can do about it. Word is fine. You just feed it a fake password and it excepts if the password is wrong. Unfortunately, Powerpoint doesn't allow you to do that unless you use it's ProtectedWindowView. This will raise an exception if a password is needed. However, there are two problems with it:

 

  • It is only available for 2010 onwards (minor issue)
  • It's really clunky as it visibly opens Powerpoint and there is no way of stopping it doing that as far as I can see (deal breaker for me). 

 

So my idea was to deal with each Powerpoint document in a separate thread. If after a period of time the thread doesn't terminate I can assume it has hung due to password issues, but then I need some way of killing it.

Share this post


Link to post
1 hour ago, Mark Williams said:

kill a hung thread

Variations of this question has been asked countless times. Always with the same answer: Don't do it.

Why would you expect a different advice this time?

 

Move the conversion into a separate process and kill that process instead.

  • Like 2

Share this post


Link to post
19 minutes ago, Anders Melander said:

Variations of this question has been asked countless times. Always with the same answer: Don't do it.

Why would you expect a different advice this time?

 

Move the conversion into a separate process and kill that process instead.

Hadn't thought of a separate process. That's an excellent idea. Thanks

Share this post


Link to post

You can't do what you ask. You need the isolation offered by processes in order to robustly kill something. You can't kill threads safely. All those other topics that you found that said the same are still a cue. 

Share this post


Link to post

Are you sure? Process Explorer can kill a selected thread... If you want to do it programmatically, there's either straight-forward ways given you have the required privileges, but I've also seen code somewhere where you declare your process as a debugger for another process that gives you access to interrupting (and then no longer resuming it), but that's quite involved.

Share this post


Link to post
17 minutes ago, David Heffernan said:

TerminateThread does the job. But do expect your process to fail as a result. 

Uhm... Are you sure? You said above it couldn't be killed. However, if it works it would solve the problem. I would only be killing the thread in circumstances where the thread had been cause to hung by MS Office. 

 

I may just go with the clunky solution I referred to in original post and only allow Office to try and open powerpoint where Office 2010 is installed.

Share this post


Link to post
10 minutes ago, David Heffernan said:

You can't kill it reliably and robustly. You can kill it, but expect consequences in your process. Noted. Will proceed the clunky route as mentioned above!

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×