Reputation:
I have an C# application runing at the back ground. Now i want to stop this application when the system is locked. how can i do that. Any help regarding this is really appreciated.
Thanks Hougen for the solution. could you please suggest me should we include any Dlls to handle "Microsoft.Win32.SystemEvents.SessionSwitch" this event? And in which layer this code should reside. I guess it is in Business layer. Any sugeestion regading this?
Upvotes: 1
Views: 1161
Reputation: 19627
Easy. Make an event handler for the
Microsoft.Win32.SystemEvents.SessionSwitch
event. In it, check the SessionSwitchEventArgs.Reason
property for the value SessionSwitchReason.SessionLock
.
Shyam: sorry for not coming back to you right away. You shouldn't have to include any special DLLs. The SystemEvents
class is in the System assembly. Whether this handler belongs in the business layer - I guess it belongs in whatever project contains your service class - the one that inherits from WindowsService.
public MyService()
{
InitializeComponent();
SystemEvents.SessionSwitch += SystemEvents_SessionSwitch;
}
void SystemEvents_SessionSwitch(object sender, SessionSwitchEventArgs e)
{
if (_isRunning)
{
// Pause
}
}
Upvotes: 9