2

Possible Duplicate:
C# Sharp Windows Application prevents Windows from shutting down / logging off

I want to write code for automatically exit from my application when user shutdowns the computer. I have done the project in C# developer. Now I can't shutdown the computer without manually exiting the application.
Please advice.

Community
  • 1
  • 1

2 Answers2

1

You may use SystemEvents class described in below link :-

http://msdn.microsoft.com/en-us/library/microsoft.win32.systemevents.aspx

Subscribe to SessionEnded event which triggers when User logs off or shut down system.

you may write a code in this event to terminate the application by itself. for eg. Application.Exit() closes the application.

triksma
  • 43
  • 6
0

You can use the SystemEvents. Just subscribe to SessionEnded and perform any closing operation you want for your application.

Word of Caution : I think you wont get enough time there to perform any complex task. But since you just want to exit from your application, just call Application.Exit() in the event handler.

Prateek Singh
  • 863
  • 1
  • 8
  • 28