Solved

TerminateThread while using COM

Posted on 2000-03-14
8
257 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Is AWS.RDS considered a part of "Big Data"? 4 128
What are the big features of MVC5? 4 95
Question to Pivot table 1 66
Where did System.Data.Objects go? 2 56
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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 goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

685 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