开发者

cleaning up unmanaged c++ thread on c# application exit

开发者 https://www.devze.com 2023-03-04 22:34 出处:网络
Here\'s my setup: 1) c# application st开发者_运维知识库arts up and calls an exported unmanaged c++ dll function

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:

  1. Set a flag visible to the thread.
  2. Call WaitForSingleObject on the HANDLE returned by CreateThread. This will make it wait for the thread to exit.
  3. Optionally be a good citizen and call CloseHandle on the thread's HANDLE to free its resources, though this doesn't really matter if the app is about to exit.
  4. Periodically check this flag inside your thread to see if it should exit the loop.
0

精彩评论

暂无评论...
验证码 换一张
取 消