SystemEvents.SessionEnding not firing

前端 未结 2 1197
天涯浪人
天涯浪人 2021-01-17 03:20

I am developing an windows forms application in c# .net 4.0. I want to capture windows logoff event.

Here is the code:

    public Form1()
    {
              


        
相关标签:
2条回答
  • 2021-01-17 03:55

    This could be useful to someone.

    if form close event is included

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
        }
    

    then SessionEnding will not be fired, i just encoutered this problem and rectified it

    void SystemEvents_SessionEnding(object sender, Microsoft.Win32.SessionEndingEventArgs e)
        {}
    

    here i need to prevent Form close upon Alt+F4 command, so i added this form closing event this resulted in this issue. so we can integrate session ending event in form close event. Option 2 in Refered from stackoverflow answer

    0 讨论(0)
  • 2021-01-17 04:01
    1. It depends on the configuration that is set on gpedit.msc.

    Open gpedit.msc, navigate to Computer Configuration > Administrative Templates > System > Shutdown Options and choose Turn off automatic termination of applications that block or cancel shutdown. Since mine laptop configure make it automatic shutdown, so it will never fire session ending

    1. Perhaps you can move your code above into entry point of its windows (in the main).

    2. Perhaps you can override windows message. You can see it in MSDN library documentation. http://msdn.microsoft.com/en-us/library/microsoft.win32.systemevents.sessionending.aspx

    3. Shutdown message pump has been re route by other software and not re route to your apps

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