none
How can I delay the machine log off in Windows Service? RRS feed

  • Question

  • Hi, I've implemented a Windows service using C#. I've implemented the protected override void OnSessionChange(SessionChangeDescription changeDescription) method, handling the reasons: SessionLogOn o SessionLogOff. On the log off, the service must do some operations that require some time interval. 

    I've already implemented a procedure who delay the machine shutdown.

    How can I delay the user machine log off, in order to give to the Windows service additional time to end the operations?

    Thank you

    Friday, February 28, 2020 11:17 AM

All replies

  • You could try the following

    https://stackoverflow.com/questions/984881/get-log-off-event-from-system

     using Microsoft.Win32;
    
     //during init of your application bind to this event  
     SystemEvents.SessionEnding += 
                new SessionEndingEventHandler(SystemEvents_SessionEnding);
    
     void SystemEvents_SessionEnding(object sender, SessionEndingEventArgs e)
     {
         if (Environment.HasShutdownStarted)
         {
             //Tackle Shutdown
         }
         else
         {
             //Tackle log off
         }
      }


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, February 28, 2020 11:24 AM
    Moderator
  • You could try the following

    https://stackoverflow.com/questions/984881/get-log-off-event-from-system

     using Microsoft.Win32;
    
     //during init of your application bind to this event  
     SystemEvents.SessionEnding += 
                new SessionEndingEventHandler(SystemEvents_SessionEnding);
    
     void SystemEvents_SessionEnding(object sender, SessionEndingEventArgs e)
     {
         if (Environment.HasShutdownStarted)
         {
             //Tackle Shutdown
         }
         else
         {
             //Tackle log off
         }
      }


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    This is not what i'm looking for. ServiceBase still contain a method who can manage the session change. What I want is to DELAY the machine log off

    protected override void OnSessionChange(SessionChangeDescription changeDescription)
    {
      try
      {
        switch (changeDescription.Reason)
         {
            case SessionChangeReason.SessionLogon:
            //....
            break;
            case   SessionChangeReason.SessionLogoff:
            //
            break;
            default:
            break;
           }                
        }
       catch (Exception ex)
         {
                    
         }
    }
    


    Friday, February 28, 2020 11:36 AM
  • Firstly I'd say a service is not the correct option here. Services are designed to run whether a user is logged on or not. If you need to track user logon/logoff then a service is not the correct solution. You need to write a normal app and simply respond to the close request (which you can tell is a logoff or not).

    Second, the method you mentioned is called when the session changes and therefore is your notification that the logoff is occurring. Are you saying that the user is getting logged off before this method completes? Any code you want to execute on logoff would happen here so there shouldn't be any issues. But note that if this is going to take more than a handful of seconds you need to really capture the logoff data you need and push it to something that can then process it after the logoff (perhaps another thread in your service) because you don't want the user sitting at a logging off screen. That is just bad design.


    Michael Taylor http://www.michaeltaylorp3.net

    Friday, February 28, 2020 2:53 PM
    Moderator
  • Second, the method you mentioned is called when the session changes and therefore is your notification that the logoff is occurring. Are you saying that the user is getting logged off before this method completes? Any code you want to execute on logoff would happen here so there shouldn't be any issues. 

    Michael Taylor http://www.michaeltaylorp3.net

    That's why I've post this question. Note that I've already implement a procedure that delay the machine shutdown (I've insert a GPO on shutdown who execute a script when the machine is shutting down). So the code in OnStop() service method is executed. But I'm not able to do that if a user logoff to the machine. 
    Monday, March 2, 2020 9:58 AM
  • Hi nopatch4humancruelty,
    I viewed your description, you can refer to jrich's explanation in this link.
    [Delay Windows 7 logoff time]
    Best Regards,
    Daniel Zhang

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, March 6, 2020 7:07 AM