?
Solved

TerminateThread while using COM

Posted on 2000-03-14
8
Medium Priority
?
262 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 20 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

Technology Partners: 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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
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 difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

777 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