473,394 Members | 1,737 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Handle SessionSwitchEvent with Service

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 ?

--
Michel
Feb 26 '07 #1
2 3345
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....IsADaemon.html)
normally runs in a different Window Station
(http://pluralsight.com/wiki/default....IsAWindowStati
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....oDisplayAUserI
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 (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.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/subscripti...t/default.aspx.
==================================================

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

Feb 27 '07 #2
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.
--
Michel
"Walter Wang [MSFT]" wrote:
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....IsADaemon.html)
normally runs in a different Window Station
(http://pluralsight.com/wiki/default....IsAWindowStati
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....oDisplayAUserI
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 (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.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/subscripti...t/default.aspx.
==================================================

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

Feb 27 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Simon Niederberger | last post by:
Hi I've written a Windows Service which has - several (0-100) listeners threads which spawn worker threads based on events, timers etc - several (0-300) worker threads which handle data...
0
by: Tiro | last post by:
Hi, I am trying to understand how does .Net Web Service handle requests for multiple clients simultaneously. Here is what I am trying to achieve. I have created a web serive that search...
1
by: Patrick Dugan | last post by:
Is it possible to get the handle of a running service? I have a program (ActiveX program) running in memory. When I start my service I need to pass the service's handle to that program in order...
3
by: Hans Merkl | last post by:
Hi, I am helping to build a web app that's pretty much a wrapper around a web service. The question now is how to store the handle of the web service object between requests. My client is using...
2
by: Max | last post by:
Hi, I am helping to build a web app that's pretty much a wrapper around a web service. The question now is how to store the handle of the web service object between requests. My client is using...
2
by: Stuart Ferguson | last post by:
I currently have a service which is written using C++/MFC and this service calls multiple dlls. This service uses these dlls to pass messages to another service via sockets. I am about to write...
6
by: Liming | last post by:
Hi, In a typical 3 tier model (view layer, busines layer and data access layer) where do you handle your exceptions? do you let it buble up all the way to the .aspx pages or do you handle it in...
5
by: =?Utf-8?B?R3V5IENvaGVu?= | last post by:
Hi all I use vb.net 2005 Lets say I have the handle number of a progress bar from program1.exe Can I change the value of the progress bar from program2.exe using this handle number? TIA Guy...
5
by: Eric Kaplan | last post by:
In C++, there is datatype HANDLE void* HANDLE then HANDLE is similar to "file handle"? which is unique identifier for a file, for a process, for a thread ... etc which is returned by a...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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
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...

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.