cleaning up unmanaged c++ thread on c# application exit
Here's my setup:
1) c# application st开发者_运维知识库arts up and calls an exported unmanaged c++ dll function 2) the dll function spawns a thread via win32 CreateThread 3) this new thread does it's "work" in a while loop, checking for an exit flag When I exit the c# application, the thread exits immediately. Questions: 1) What can I do to allow my thread to cleanup before exiting? Much thanks - I am new to the c# world, but experienced with c++When your C# app exits:
- Set a flag visible to the thread.
- Call
WaitForSingleObject
on theHANDLE
returned byCreateThread
. This will make it wait for the thread to exit. - Optionally be a good citizen and call
CloseHandle
on the thread'sHANDLE
to free its resources, though this doesn't really matter if the app is about to exit. - Periodically check this flag inside your thread to see if it should exit the loop.
精彩评论