Click here to Skip to main content
15,895,011 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi there
i create a windows service in c#.
i must detect when user logoff from system.
i think solution is finding the first event of system along user logoff operation.
please guide me.
thanks
Posted

1 solution

There is Session ending event in which you can handle shutdown or log off

C#
using Microsoft.Win32;

 // you need to init this on service start
 SystemEvents.SessionEnding += 
            new SessionEndingEventHandler(SystemEvents_SessionEnding);


 void SystemEvents_SessionEnding(object sender, SessionEndingEventArgs e)
 {
     if (Environment.HasShutdownStarted)
     {
         //Handle shutdown
     }
     else
     {
         //Handle log off
     }
  }
 
Share this answer
 
Comments
aliwpf 4-Feb-16 3:22am    
before this question i try above code. this is wrong . because this event is raised after shutdown. it's too late .

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900