Terminate a worker thread that starts a message loop in C #

I used the SetWindowHookEx() API in the SetWindowHookEx() and to receive OS notifications. I ran Application Message Pump using Application.Run() .

Now I want to stop the message pump and exit the workflow, but I have no good way to stop the flow.

Alternatively, I use the Thread.Abort() method to end the thread, but it is not recommended to end the thread gracefully.

Can anyone suggest how to gracefully close the message pump from the user interface stream or main form so that the workflow automatically returns and exits?

+4
source share
3 answers

Instead of using a parameter without Application.Run parameters, use an overload that accepts ApplicationContext .

The application context has an ExitThread method that gracefully completes the message loop.

+4
source

Why not just call Application.Exit from another thread?

0
source

if you gracefully handle a ThreadAbortException that Thread.Abort () will throw in a worker thread, there is nothing ungrateful in interrupting the thread.

-2
source

All Articles