Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 472
user642770 Avatar asked Jun 23 '11 09:06

user642770


2 Answers

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.

like image 148
ChrisF Avatar answered Oct 10 '22 13:10

ChrisF


Handled events will not bubble up to root control. Instead you should use the AddHandler method with handledEventsToo = true.

like image 44
Dom Avatar answered Oct 10 '22 14:10

Dom