C # Windows app prevents Windows shutdown / shutdown

I wrote a C # Windows Forms application, not a service (it is used only when the user is logged in and has a graphical user interface) that has a background thread that runs in an endless loop.

However, when I try to turn off Windows (7), it tells me that the program does not allow it to shut down or exit the system and asks me if I want to force shutdown.

Now, is it possible for my program to recognize (get a handler) Windows trying to exit it or exit the system?

So, I need the application to be implemented when Windows tries to exit.

Thanks in advance.

EDIT: Thanks for the great tip! Is there any way to use the idea with a form closing event if it has a CANCEL event handler?

+4
source share
4 answers
public Form1() { InitializeComponent(); this.FormClosing += new FormClosingEventHandler(Form1_FormClosing); } void Form1_FormClosing(object sender, FormClosingEventArgs e) { // Or any of the other reasons suitable for what you want to accomplish if (e.CloseReason == CloseReason.WindowsShutDown) { //Stop your infinite loop } } 
+10
source

You call this stream a "background stream", but does it have IsBackground = true; ?
The system will stop only the thread that is doing.

+5
source

I think Capture exit exit C # can also be used in your script.

Also, maybe you just need to configure the stream as a background stream ?

+2
source
+1
source

Source: https://habr.com/ru/post/1304848/


All Articles