473,503 Members | 2,313 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SessionEnding Event in Window Service not working

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
1 5538
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
4893
by: Pai | last post by:
hello there, I am trying to rersize the window it works find in IE but doea not work with mozilla window.attachEvent(onload,MWSOnLoad); window.onload = function (MWSOnLoad) { alert('hello');...
10
9760
by: Tom Szabo | last post by:
Is there an event when that triggers when the window is closing.... I am talking about when the user clicks on the cross on the right top corner of the window!!!
0
361
by: Manfred Braun | last post by:
Hi All, I am trying to fetch system-events, and I am successful with "TimeChanged", but cannot get "SessionEnding". I am not an OO expert and so - just to be sure - I made all my methods public...
0
2842
by: Roshan | last post by:
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...
4
4788
by: Bill Manring | last post by:
I need to capture the event when the user closes the browser in my application. I have some code in the session_End event, which works fine when the session times out, but I need to end the...
5
2922
by: bbembi_de | last post by:
Hello everyone, I'm listening to the SessionEnding event, so I can close my application properly when Windows is shutting down. I do some disposing and stuff if the event occurrs but windows...
2
6499
by: Linda Liu[MSFT] | last post by:
Hi George, Keyboard input introduces the idea of focus. In Windows, a particular element is designated as having the focus, meaning that it acts as the target for keyboard input. The user sets...
1
2673
by: anil | last post by:
I want to capture key down event in the window service and open a new app when the user press F7. Is there any solution available in C#
1
1697
by: ashu2409 | last post by:
Hi, i have a window service in C#,in which i am fetching the html from diff URLs, now i need to click the button on html page for pagination. i tried with web browser, but window service is...
0
7070
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7267
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7316
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6976
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5566
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4993
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3148
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
729
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.