Handle SessionSwitchEvent with Service

G

Guest

Hi all,

I try to create a service witch handle the session switch events: Lock,
unlocked etc...
I can do this with a standard executable like this (it's just an example):
//****************************************
//****************************************
public class Event
{
static WaitHandle[] listhandle = new WaitHandle[] { new
AutoResetEvent(false) };

static public void Main()
{
SessionSwitchEventHandler handler = new
SessionSwitchEventHandler(EvHandler);
SystemEvents.SessionSwitch += handler;
WaitHandle.WaitAny(listhandle);
}

static void EvHandler(object obj, SessionSwitchEventArgs args)
{
string str = args.Reason.ToString() ;
Console.WriteLine(str);
}
}
//****************************************
//****************************************

however, when i try to use the same way for my service (i replace the
Console.writeline() by a eventlog entry) , nothing is write !!
The service start and run normally but if i locked and unlock my session
nothing is write in my eventlog.

The following is a example of what i try :
//****************************************
//****************************************
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
SessionSwitchEventHandler handler = new
SessionSwitchEventHandler(EvHandler);
}

private void EvHandler(object obj, SessionSwitchEventArgs args)
{
string str = args.Reason.ToString();
this.EventLog.WriteEntry(str, EventLogEntryType.Information);
}

protected override void OnStart(string[] args)
{

}

protected override void OnStop()
{

}

}
//****************************************
//****************************************

The session Events are not handle by this way, have you any ideas ?
 
W

Walter Wang [MSFT]

Hi Michel,

To understand why the event is not fired in your service, we need to first
understand two concepts here:

1) The session switch event is fired when fast user switching feature is
enabled (such as on Windows XP). The event is actually a windows message
broadcasted to all top-level windows. The SystemEvents class in .NET
Framework 2.0 internally creates a hidden window to receive these broadcast
messages and fire managed events accordingly.

2) A service (see
http://pluralsight.com/wiki/default.aspx/Keith.GuideBook/WhatIsADaemon.html)
normally runs in a different Window Station
(http://pluralsight.com/wiki/default.aspx/Keith.GuideBook/WhatIsAWindowStati
on.html) other than the interactive Window Station "WinSta0", which is
where your logon session belongs to.

A Window Station is a securable object that is associated with a process,
and contains a clipboard, an atom table, and one or more desktop objects.

A desktop is a securable object contained within a window station. A
desktop has a logical display surface and contains user interface objects
such as windows, menus, and hooks.
(http://msdn2.microsoft.com/en-us/library/ms681928.aspx)

A broadcast message can only send to all top-level windows in the same
desktop. (http://www.flounder.com/messaging.htm)

Unless you put your service in the interactive Window Station (WinSta0) too
(http://pluralsight.com/wiki/default.aspx/Keith.GuideBook/HowToDisplayAUserI
nterfaceFromADaemon.html), your service will not be able to receive the
session switch message.


Hope this helps. Let me know if you need further information. Thanks.


Sincerely,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

Your help and documentations were very helpful.
Indeed, Now I see that I had a problem of philosohie.
I think that in the case of the development of a service it is necessary to
"thinking†directly in service development and not to translate executable
developement to service.
Thank you for your assistance on the “philosophy†of the service.
 

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