views:

243

answers:

1

Hi all,

I have created a windows service in C# .net and now I want it to alert me if its going to shutdown. For this I make use of Dispose/Unload events but now I want if the services are stopping due to system shutdown process it will alert me.

So is there any way to get access to system shutdown event and execute the set of commands in it further till the command executes the system remain active booted state and only shut down when the task completes.

Further enhancement to it if I can run it without properly loading of system for example like avast boot time scan

Any help for the above is urgently needed

with regards,

Ankur

A: 

I know my answer is too late, but I just see this question right now :)

You can handle the system event:

Microsoft.Win32.SystemEvents.SessionEnded

This event will be raised when you shutdown your system or log off from your system. Hadnle it to do whatever logic you need.

Example:

@OnStart() of your service add the following code:

Microsoft.Win32.SystemEvents.SessionEnded += new Microsoft.Win32.SessionEndedEventHandler(SystemEvents_SessionEnded);

then add the event handler:

void SystemEvents_SessionEnded(object sender, Microsoft.Win32.SessionEndedEventArgs e)
        {
            //put you logic here
        }
Sameh Serag