Solved

TerminateThread while using COM

Posted on 2000-03-14
8
255 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
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.
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.

816 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

11 Experts available now in Live!

Get 1:1 Help Now