473,503 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to detect session start and stop?

My program needs to do X when someone 'starts using' their Windows user
account, and it should do Y when they 'stop using' their Windows user
account. By 'starts using' I mean they log on, unlock the desktop, resume
from hibernate/sleep, or resume a session that was paused via Switch User. By
'stop using' I mean they lock the desktop, initiate a hibernate/sleep
, or choose Switch User while logged on. For context, the program is a
parental control application that helps parents limit the total time their
kids use the computer. There is a Windows service that does the actual time
keeping. The program I'm struggling with is the reminder program that gives
the child audible reminders like "5 minutes left". It should tell them how
much time is left when they start using Windows, and it should be quiet when
they take a break.

This sounds like a simple task, but I can't get it to work reliably.

My current approach is to put the program in the child's StartUp folder.
When the application starts, it does X. It then listens to the events from
SystemEvents.SessionSwitch and SystemEvents.PowerModeChanged and does X if it
detects a start event like Unlock and does Y if it detects a stop event like
Suspend.

The first problem is that if you log on and then immediately Lock the
computer or do a Switch User, then by the time my application has fully
loaded it may be running in the context of a locked workstation. So it
shouldn't automatically do X (tell the child how much time is left) when the
application starts. Somehow my program needs to know if it is running in the
context of a locked or switched out Windows session. I don't know how to
check for that.

The second problem is that interpreting the SystemEvents.SessionSwitch and
SystemEvents.PowerModeChanged events is tricky. For example, if Windows is
configured to require a password after resuming from sleep, then doing a
hibernate followed by resume leads to these events:
Suspend->Lock->Resume->Unlock. But if Windows is not configured to require a
password after resuming from sleep, then these events are generated:
Suspend->Resume. Therefore I can't simply look to the Resume event to tell me
if the user has started using Windows again. Likewise if you do a Switch user
and then resume, these events are generated: Disconnect->Connect. But if you
do a lock, switch out, switch in, these events are generated:
Lock->Disconnect->Connect->Unlock. Therefore I can't simply look to the
Connect event to tell me if the user has started using Windows again.

Here's my algorithm:

I initialize a variable called "Depth" to 0.
Lock, Disconnect, Suspend all decrement "Depth" by 1.
Unlock, Connect, Resume all increment "Depth" by 1.
When "Depth" changes to 0, that means the user has started using Windows.
When "Depth" changes to -1, that means the user has stopped using Windows.

This works most of the time but if the users session is switched out soon
after they log on and before my application has fully loaded and subscribed
to the events, then "Depth" should not really be initialized to 0. It should
start at -1 or -2. And since I can't properly detect the initial state, I
can't properly interpret all future SessionSwitch and PowerModeChanged events.

How can I reliably do X when someone starts using Windows, and do Y when
they stop?
How can I know whether my program is running in the context of a locked or
switched out user session?
Sep 12 '08 #1
0 3269

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

Similar topics

1
7768
by: Paul | last post by:
Hmmm, didn't seem to work. I have set session.use_cookies = 1 and session.use_trans_sid = 1 in my php.ini file. Index.php contains:...
15
3561
by: Manish Jethani | last post by:
Hi all, Is there a way to detect typos in a Python program, before actually having to run it. Let's say I have a function like this: def server_closed_connection(): session.abost() Here,...
0
1344
by: Cédric | last post by:
Hi, I have some problems to detect when a file is changed by another application (the file is changed several times by the application). Sometimes, it is correctly detected, sometimes not. I...
4
5784
by: sanjana | last post by:
hi can anyone help me with this i want to write a code in c#.net which detects usb storage and media card insertion can anyone help me with this?? thanx
4
1503
by: Chad | last post by:
I rebuilt my pc (format and reinstall) running XP and Visual Studio 2003. My problem is that the IDE won't stop the run session when I close the web application. Now when I create a new...
4
1889
by: Harri Pesonen | last post by:
I have an ASP.NET application that runs threads in background. Application does not close if the web site is stopped. So how can the application get an event when the site has been stopped, so that...
2
9248
by: Cam | last post by:
Hi to all I have searched this subject and found some posts but none of them seem to answer my problem. I am developing a shop site and need to detect when a session times out through...
5
1663
by: Terry On Windigo | last post by:
I think I have figured out my problem but I don't know how to solve it. We are going to start using a forums package and do not want our users to have to login to both our site, and then again to...
5
2562
by: jediknight | last post by:
Is it possible to programmatically stop multiple browsers from getting the same session state? The problem I have is that whenever I open a new browse using File- I would like to stop this...
0
7203
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
7281
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
7334
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
6993
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
5579
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,...
0
3168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3156
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1514
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
737
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.