views:

384

answers:

1

Hi,

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?

+9  A: 

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
    }
}
Tor Haugen
Thanks alot. I will try with this approach and let you know.
shyam
should we include any Dll's to handle this event?
shyam
Can you suggest me in which layer this event should be raised?
shyam
i guess it is Business layer please confirm me!!!
shyam