I have this existing code that doesn't properly clear the thread on dispose. First off, to start the thread that waits for incoming signals and process them:
public void StartTheWholeThing(){
manualResetEvent = new ManualResetEvent();
thread = new Thread(StartRunning);
thread.start();
manualResetEvent.Set();
}
public void StartRunning()
{
//Listen for foreground changes
hhook = SetWinEventHook(EventSystemForeground, EventSystemForeground, IntPtr.Zero,
ProcessDelegate, 0, 0, WineventOutofcontext);
// Thread state
manualResetEvent.WaitOne();
// mesage loop that SetWinEventHook requires
Application.Run();
}
private void ProcessDelegate(){
//Do something
}
Now we want to properly close the thread.
public void StopRunning(){
manualResetEvent.Reset();
thread.Abort(); // IS THIS THE CORRECT MANNER?
}
Thread.Abort() seems a rough way to close the thread. Is there a more clean way to start/stop threads in combination with the SetWinEventHook?