Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / February 2007

Tip: Looking for answers? Try searching our database.

SessionEnding Event in Window Service not working

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
chitra g - 27 Feb 2007 17:36 GMT
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_SessionEnding);

           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");

       }

   }

}
Laura T. - 28 Feb 2007 10:26 GMT
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.

> Hi,
>
[quoted text clipped - 161 lines]
>
> }

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.