SystemEvents.SessionEnding not working

R

Roshan

Hi All,

Iam using WMI for the first time and am trying to detect and stop a
user login event. But when i run my code, i notice that the event
handler is not getting called. Can some one tell me where Iam going
wrong?

Thanks a million,
Roshan

The code i used is as follows

using Microsoft.Win32;
using System;
using System.IO;
using System.Management;

namespace myWmi
{
public class WMITest
{
public static void Main(String[] args)
{
WMITest wm= new WMITest();

// Register the event handler
SystemEvents.SessionEnding += new
SessionEndingEventHandler(ShutdownStarted);

// Wait for a logout/shutdown event
Console.WriteLine("Waiting for Events");
Console.ReadLine();
}

static void ShutdownStarted(object sender,
SessionEndingEventArgs e)
{
e.Cancel = true;
Console.WriteLine("In SessionEndingEventHandler");
using(StreamWriter sw = new
StreamWriter("C:\\Shutdown.txt"))
{
sw.WriteLine("System is going down for a
{0}.",e.Reason.ToString());
sw.WriteLine("Cancelling End of session");
}
}
}// End of class WMITest
}// End of namespace
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top