473,406 Members | 2,387 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,406 software developers and data experts.

Windows lock/unlock (with code)

11
Hi everyone,

i'm trying to Handle the Micosoft.Win32.SessionSwitch Event, so when Windows is going to be locked by the user, i will simply write a line to the console.When it's going to be unlocked, the same.

Here's the code:
Expand|Select|Wrap|Line Numbers
  1.     Sub Main()
  2.         AddHandler Microsoft.Win32.SystemEvents.SessionSwitch,
  3. _ SystemEvents_Sessionswitch()
  4.         Console.ReadLine()
  5.         RemoveHandler Microsoft.Win32.SystemEvents,
  6. _ SystemEvents_Sessionswitch()
  7.     End Sub
  8.  
  9.     Private Sub SystemEvents_Sessionswitch(ByVal sender As Object, 
  10. _ ByVal e As Microsoft.Win32.SessionSwitchEventArgs)
  11.         If e.Reason = Microsoft.Win32.SessionSwitchReason.SessionLock Then
  12.             Console.WriteLine("locked at: " & DateTime.Now)
  13.         End If
  14.         If e.Reason = Microsoft.Win32.SessionSwitchReason.SessionUnlock Then
  15.             Console.WriteLine("unlocked at: " & DateTime.Now)
  16.         End If
  17.     End Sub
  18.  
My Problem is now, that the Add/Remove Handler wants to have the 2 arguments given.

Here's the code in C# again, here it works flawless, without the arguments given.

Expand|Select|Wrap|Line Numbers
  1. using Microsoft.Win32;
  2. public class App
  3. {
  4. static void Main()
  5. {
  6. SystemEvents.SessionSwitch += SystemEvents_SessionSwitch;
  7. Console.ReadLine(); 
  8. SystemEvents.SessionSwitch -= SystemEvents_SessionSwitch;
  9. }
  10.  
  11. static void SystemEvents_SessionSwitch(object sender, SessionSwitchEventArgs e)
  12. {
  13. if(e.Reason == SessionSwitchReason.SessionLock)
  14. {
  15.     Console.WriteLine("locked at {0}", DateTime.Now);
  16. }
  17. if(e.Reason == SessionSwitchReason.SessionUnlock)
  18. {
  19.     Console.WriteLine("unlocked at {0}", DateTime.Now);
  20. }
  21. }
  22. }
Thank you and sorry for my bad englisch.

Chris
Mar 5 '09 #1
6 15124
chrisli
11
Ok, i'm an idiot. :)

Here's the solution:

Expand|Select|Wrap|Line Numbers
  1.         AddHandler Microsoft.Win32.SystemEvents.SessionSwitch, _
  2.  AddressOf SystemEvents_Sessionswitch
  3.         Console.ReadLine()
  4.         RemoveHandler Microsoft.Win32.SystemEvents.SessionSwitch, _
  5.  AddressOf SystemEvents_Sessionswitch
  6.  
The AddressOf was missing. :)
Mar 5 '09 #2
i am using the "chrisli" code which is given in C# i made a dot net service i will work fine when i login the system and then start it will capture all the event .

The problem is that when i restart the system and login (it will not capture the logon event,lock,unlock) . if i restart the service it will work fine and capture all event like (logoff,logon,lock , unlock)
Oct 6 '09 #3
Plater
7,872 Expert 4TB
The C# code is a bit flawed.
Eventhandlers should be added like
Expand|Select|Wrap|Line Numbers
  1. SystemEvents.SessionSwitch += new SessionSwitchEventHandler(SystemEvents_SessionSwitch);
  2.  
And removed with the -=
Expand|Select|Wrap|Line Numbers
  1. SystemEvents.SessionSwitch -= new SessionSwitchEventHandler(SystemEvents_SessionSwitch);
  2.  
(There are other ways, but simply using += SystemEvents_SessionSwitch should be avoided)


Are you removing the eventhandler when you shouldn't be?
Oct 6 '09 #4
i have done this but the code is running condition and work in service when i login the system and restart the service in this scenario it will work fine

but the problem is that when i restart the computer and logon,logof,lock,unlock it will not capture these event
Oct 7 '09 #5
Plater
7,872 Expert 4TB
And you are sure your service is running and not paused or busy?
Hmmm
Oct 7 '09 #6
i have find the solution.the SystemEvent depend on TermService(Terminal Service) i set my service dependence on TermService now it will work fine
Oct 7 '09 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: James | last post by:
Im having trouble building/running a program which was written under Visual Studio 2000 & .NET Framework 1.0. Im now using a new installation of .NET 1.1 with Visual Studio 2003, otherwise there...
1
by: kufre | last post by:
I need some help on two problem I'm working on. First, I have an option group button with a checkbox called Inactive that allow me to open up a new blank form and input new record, but the problem...
6
by: deko | last post by:
Now that users are handy at importing tables from an Excel spreadsheet, I've discovered a very dangerous problem. If the user decides to name the imported table the same name as an existing...
8
by: Komandur Kannan | last post by:
We have a smart device application running on handhelds(Symbol MC9000G). The backend is Oracle and a middle tier web services development done in Vb.net. We use pessimistic Locking due to...
0
by: vve | last post by:
I'm discovering a strange behaviour in an C# project using ZedGraph (https://sourceforge.net/projects/zedgraph/). After adding a signal to it, it seems that the clr goes mad for some reason. I...
3
by: The Frog | last post by:
Hi Everyone, I have a need to build a simple tool in Access 97 for my work that will allow someone to convert a database (supplied externally) back to an Access 97 format mdb file. The files...
0
by: tierrczjlfbaum | last post by:
unlock registration code crack
11
by: minishilpi | last post by:
I have a question - I have this code below to send an email and I have referenced the Microsoft CDO Library 2000. It doesn't throw any exception in the console window. It goes throughout the code...
7
mseo
by: mseo | last post by:
hi, I have Follow-Up report for orders and deliveries to calculate the Build Quantities (the quantity I have to produce to fill the order) after subtracting the quantities delivered to the customer...
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?
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
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
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
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...
0
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,...

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.