Search code examples
silverlightpython-idledispatchertimer

idle state detection silverlight 4 application


What's the best way to detect idle state for a silverlight application? I have read quite a few articles on the net by now and usually they are either for wpf/mobile apps etc.

I have created a DispatcherTimer which locks the screen after 5 minutes and it seems that I will have to go to every widget in every screen(my application has around 4-5 screens) and add a mousebuttondown or mouseenter eventhandler to reset this timer. This doesn't seem to be efficient but just adding the handler to the layroot is not helping either.

Any helpful suggestions?

Thanks


Solution

  • You don't need to modify every control. If you add the following code on startup:

    Application.Current.RootVisual.MouseMove += new MouseEventHandler(RootVisual_MouseMove);
    Application.Current.RootVisual.KeyDown += new KeyEventHandler(RootVisual_KeyDown);
    

    With the following event handlers:

    private void RootVisual_KeyDown(object sender, KeyEventArgs e)
    {
        idle = false;
    }
    
    private void RootVisual_MouseMove(object sender, MouseEventArgs e)
    {
        idle = false;
    }
    

    Where idle is the variable you use in your DispatcherTimer Tick event to check if things are happening or not.

    As events bubble up the tree this should work for all your controls.