Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
when the user press window+L key, C# application will close automatically

What I have tried:

I have try to use Lockworkstation method but I am unable to do that
Posted
Updated 15-Apr-17 2:51am
Comments
Dave Kreskowiak 15-Apr-17 10:27am    
Better question: Why would you want to?

 
Share this answer
 
// check below code it will automatically close the application whenever you locks the workstation by windows+L key, hope it solves your problem

using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication4
{
    public partial class Form1 : Form
    {
        private static SessionSwitchEventHandler eHandler;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            eHandler = new SessionSwitchEventHandler(EventCapture);
            SystemEvents.SessionSwitch += eHandler;
        }
        void EventCapture(object sender, SessionSwitchEventArgs e)
        {

            switch (e.Reason)
            {
                case SessionSwitchReason.SessionLock: MessageBox.Show("Lock Encountered"); this.Close(); break;
                case SessionSwitchReason.SessionUnlock: MessageBox.Show("UnLock Encountered"); break;
            }
        }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900