title: Terminating a thread gracefully not using TerminateThread()
body: <p>My application creates a thread and that runs in the background all the time. I can only terminate the thread manually, not from within the thread callback function.
  At the moment I am using <code>TerminateThread()</code> to kill that thread but it's causing it to hang sometimes.
  I know there is a way to use events and <code>WaitForSingleObject()</code> to make the thread terminate gracefully but I can't find an example about that.</p>
  
  <p>Please, code is needed here.</p>