Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how do i kill a thread in Onclose() event at CLOSE(X)
Posted
Comments
Sergey Alexandrovich Kryukov 31-Aug-15 11:12am    
First of all, why? Yes, sometimes it is needed... but this is a complicated issue.
—SA

1 solution

This is a painful and complicated issue, which triggers heated discussions from time to time. Many would tell you that you should never terminate thread; and a thread should gracefully terminate itself (just exit), after some polling of some flag (in shared data) which would give the thread an order to exit. But I rather say "never say 'never'"; unfortunately, there are whole big fields of engineering where such things are totally infeasible. I would say more: authors of OS purely recognize existence of such fields.

Please see my past answer:
Close correcly the thread inside a dll[^].

Even if you won't use it, it will give you the idea on how serious this seemingly simple topic is.

As I don't know your application, I can first advise the cooperative termination described in first paragraph. If it cannot work for you, I can even mention really dirty thing: a call to TerminateThread. Yes, it is dirty and strongly discouraged, but if the design of your thread does not allow simple cooperative termination you have one more excuse: if this is the last thing your application does, you won't have to deal with the possibly bad consequences of this call.

—SA
P.S.: It is very likely that this post will be down-voted. I would advise you to not pay attention for that. This topic is generally poorly understood.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900