By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,732 Members | 1,429 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,732 IT Pros & Developers. It's quick & easy.

SessionEnding Event in Window Service not working

P: n/a
Hi,

I tried all the options below but did not work.

Your suggestions please.

Thanks,

Chitra G

Sent: Thursday, February 01, 2007 4:59 PM
Subject: RE: SessionEnding Event in Window Service

I'm not an expert here, and I don't quite know what you mean by "save code
coverage".

But, do services actually run in a "session"? My understanding is that the
SessionEvents class deals with interactive sessions (though the docs are
skimpy).

Wouldn't setting "CanShutdown" to true in your service startup code and then
overriding OnShutdown do what you wanted?

(oh, and by the way, the docs on SessionEvents events say: "Because this is
a static event, you must detach your event handlers when your application is
disposed, or memory leaks will result.")

HTH

Brian


--------------------------------------------------------------------------------

Sent: Wednesday, January 31, 2007 3:25 PM
Subject: SessionEnding Event in Window Service

Hi,

I would like to do save code coverage across reboots automatically. So I
created a windows service and handled SessionEnding event to save the code
coverage. As the first step, in the event handler, added simple code to add
a registry key.

This event handler is not getting called on a reboot. The code of the simple
windows service is pasted below. Please note that none of the reg keys
SessionEnding , SessionEnded or Disposed is created.

Can someone help me with this issue?

If this is not the right forum, can u please point me to the right one.

Thanks,

Chitra G

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Diagnostics;

using System.ServiceProcess;

using System.Text;

using Microsoft.Win32;

namespace WindowsService1

{

public partial class Service1 : ServiceBase

{

public Service1()

{

InitializeComponent();

Registry.LocalMachine.SetValue("InCtor", "InCtor");

SystemEvents.SessionEnding += new
SessionEndingEventHandler(SystemEvents_SessionEndi ng);

SystemEvents.SessionEnded += new
SessionEndedEventHandler(SystemEvents_SessionEnded );

this.Disposed += new EventHandler(DisposedEventHandler);

}

protected override void OnStart(string[] args)

{

EventLog.WriteEntry("My simple service started.");

Registry.LocalMachine.SetValue("ServiceStarting",
"ServiceStarting");

Registry.LocalMachine.DeleteValue("SessionEnding", false);

Registry.LocalMachine.DeleteValue("ServiceStopping ", false);

}

protected override void OnStop()

{

Registry.LocalMachine.SetValue("ServiceStopping",
"ServiceStopping");

}

private void SystemEvents_SessionEnding(object sender,
SessionEndingEventArgs e)

{

// Is the system rebooting?

//if (e.Reason == SessionEndReasons.SystemShutdown)

{

// write a reg key.

Registry.LocalMachine.SetValue("SessionEnding",
"SessionEnding");

e.Cancel = false;

}

}

private void SystemEvents_SessionEnded(object sender,
SessionEndedEventArgs e)

{

// write a reg key.

Registry.LocalMachine.SetValue("SessionEnded", "SessionEnded");

}

private void DisposedEventHandler(object sender, EventArgs e)

{

Registry.LocalMachine.SetValue("Disposed", "Disposed");

}

}

}


Feb 27 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
If there is an interactive user logged on, you will get
SessionEndReasons.Logoff instead of SessionEndReasons.SystemShutdown.
If there is none, you will get SessionEndReasons.SystemShutdown.
This at least with XP and Windows 2003 Sp1/Sp2rc.

The workaround is to set a flag when getting Logoff and then if the SCM
calls for stop of your service, the real reason is a shutdown, otherwise
not.
If getting SystemShutdown.. well, it's real.
"chitra g" <go***********@hotmail.comha scritto nel messaggio
news:OP**************@TK2MSFTNGP03.phx.gbl...
Hi,

I tried all the options below but did not work.

Your suggestions please.

Thanks,

Chitra G

Sent: Thursday, February 01, 2007 4:59 PM
Subject: RE: SessionEnding Event in Window Service

I'm not an expert here, and I don't quite know what you mean by "save code
coverage".

But, do services actually run in a "session"? My understanding is that
the SessionEvents class deals with interactive sessions (though the docs
are skimpy).

Wouldn't setting "CanShutdown" to true in your service startup code and
then overriding OnShutdown do what you wanted?

(oh, and by the way, the docs on SessionEvents events say: "Because this
is a static event, you must detach your event handlers when your
application is disposed, or memory leaks will result.")

HTH

Brian


--------------------------------------------------------------------------------

Sent: Wednesday, January 31, 2007 3:25 PM
Subject: SessionEnding Event in Window Service

Hi,

I would like to do save code coverage across reboots automatically. So I
created a windows service and handled SessionEnding event to save the code
coverage. As the first step, in the event handler, added simple code to
add a registry key.

This event handler is not getting called on a reboot. The code of the
simple windows service is pasted below. Please note that none of the reg
keys SessionEnding , SessionEnded or Disposed is created.

Can someone help me with this issue?

If this is not the right forum, can u please point me to the right one.

Thanks,

Chitra G

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Diagnostics;

using System.ServiceProcess;

using System.Text;

using Microsoft.Win32;

namespace WindowsService1

{

public partial class Service1 : ServiceBase

{

public Service1()

{

InitializeComponent();

Registry.LocalMachine.SetValue("InCtor", "InCtor");

SystemEvents.SessionEnding += new
SessionEndingEventHandler(SystemEvents_SessionEndi ng);

SystemEvents.SessionEnded += new
SessionEndedEventHandler(SystemEvents_SessionEnded );

this.Disposed += new EventHandler(DisposedEventHandler);

}

protected override void OnStart(string[] args)

{

EventLog.WriteEntry("My simple service started.");

Registry.LocalMachine.SetValue("ServiceStarting",
"ServiceStarting");

Registry.LocalMachine.DeleteValue("SessionEnding", false);

Registry.LocalMachine.DeleteValue("ServiceStopping ", false);

}

protected override void OnStop()

{

Registry.LocalMachine.SetValue("ServiceStopping",
"ServiceStopping");

}

private void SystemEvents_SessionEnding(object sender,
SessionEndingEventArgs e)

{

// Is the system rebooting?

//if (e.Reason == SessionEndReasons.SystemShutdown)

{

// write a reg key.

Registry.LocalMachine.SetValue("SessionEnding",
"SessionEnding");

e.Cancel = false;

}

}

private void SystemEvents_SessionEnded(object sender,
SessionEndedEventArgs e)

{

// write a reg key.

Registry.LocalMachine.SetValue("SessionEnded", "SessionEnded");

}

private void DisposedEventHandler(object sender, EventArgs e)

{

Registry.LocalMachine.SetValue("Disposed", "Disposed");

}

}

}


Feb 28 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.