Solved

TerminateThread while using COM

Posted on 2000-03-14
8
260 Views
Last Modified: 2013-11-25
How should I terminate a thread, while the thread is using a COM service.
0
Comment
Question by:rskathait
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 5

Expert Comment

by:Wyn
ID: 2618737
Why terminate ,you'd let it end naturally or what you need ?
0
 
LVL 11

Accepted Solution

by:
mikeblas earned 10 total points
ID: 2618927
Never, ever, _ever_ call the TerminateThread() API.

..B ekiM
0
 
LVL 5

Expert Comment

by:Wyn
ID: 2618989
he he
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:decaffeinated
ID: 2626632
"mikeblas" (who incidentally is none other than the Lord God of MFC, Mr. Mike Blasczak himself!) is absolutely right about the TerminateThread() function.  That is just about the most evil function there is.  If you call that, the thread you've terminated will never clean up it's stack or release any resources it may have.  Very very bad.  If you find yourself using it, you probably need to think about redesigning your code.

The standard way to "terminate" a thread is really more like asking the thread to terminate and then waiting for it to do so.  This requires that _your_ code to be the code that starts the thread and (usually) that _you_ author the thread's thread-procedure.  If you are talking about a thread created by a third party, then these steps don't apply to you -- your probably out of luck.

Generally all the techniques I'm going to describe here require that there is some custom flag that the thread's procedure checks from time to time (call it the "kill" flag).  When the threadproc sees that flag set it knows it's time to exit.

One way involves passing some sort of handle (e.g. an event handle) to the thread at startup time and having that thread periodically wait on that handle.  When you want the thread to end, you set the event.

Alternately you could have some custom interthread message that you post to the thread via ::PostThreadMessage.  A variation on this is to post a custom window message to a window that was created by the thread.  When the thread/window receives the message it sets the "kill" flag (or does PostQuitMessage).

A third way (and this only applies to threads that perform alertable waits) is to QueueUserAPC to a custom APC function (in the thread you want to terminate) that sets some custom flag that will cause the thread procedure to exit the next time it


Joe O'
0
 
LVL 1

Author Comment

by:rskathait
ID: 2638721
Thanks mikeblas and decaffeinated. I was knowing these things and wanted to know the exact scenario like what will COM do. OK, it is dangerous to the thread but will COM shutdown cleanly?

rgds,
rskathait
0
 

Expert Comment

by:decaffeinated
ID: 2638752
If you shut down a thread via ::TerminateThread() then no, COM will not shutdown cleanly for that thread.  It allocates resources when you call CoInitialize(Ex) that will never be freed.

However if you terminate it the safe way as I described previously, then yes, COM will shutdown cleanly (provided of course your thread procedure calls CoUninitialize once for every time it previously called CoInitialize).

But regardless of which method you choose, the only damage you are likely to do will be confied to the boundaries process.  Even if you resort to TerminateThread, other processes that use COM will not be affected once your process terminates.
0
 
LVL 11

Expert Comment

by:mikeblas
ID: 2647629
> Even if you resort to TerminateThread, other processes that
 > use COM will not be affected once your process terminates.

I'd never make such a guarantee.

..B ekiM
0
 

Expert Comment

by:decaffeinated
ID: 2650447
Oops, didn't mean to make any sort of guarantee.  I should have used the word "likely" where I said "will".

(not that I would ever want anyone to use TerminateThread, though)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question