mahakaal
// 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;
}
}
}
}