Solved

TerminateThread while using COM

Posted on 2000-03-14
8
251 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
  • 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
 

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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
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…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

706 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now