C# Windows application prevents Windows from shutting down / logging off

前端 未结 4 1950
后悔当初
后悔当初 2021-01-06 09:07

I have written a C# Windows Forms application, not a service (it is only used when the user is logged in and has a graphical user interface) that has a background thread run

相关标签:
4条回答
  • 2021-01-06 09:39
    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
        }
    }
    
    0 讨论(0)
  • 2021-01-06 09:43

    I think Capture console exit C# should also be usable in your scenario.

    Apart from that, maybe it is sufficient to set up your thread as background thread?

    0 讨论(0)
  • 2021-01-06 09:47

    Take a look at the Microsoft.Win32.SystemEvents.SessionEnding event.

    0 讨论(0)
  • 2021-01-06 09:56

    You call that thread a "background thread" but does it have IsBackground = true; ?
    The system will only stop a thread that does.

    0 讨论(0)
提交回复
热议问题