Doing something in WP8 app when user does nothing for some seconds (kinda user idle mode)

84 views Asked by At

My scenario. A page of my WP8 Silverlight app contains a ListBox control with the ability of item multiselection. When the user tap an item to toggle its selection state, I should perform a long operation for all items which are selected by that moment (for example, run a query to filter by the currently selected items). The user can tap some items very quickly, and there is no need to run the query every time after each tap (especially taking into account the fact that the query can take 1-2 seconds). I need to run the query if the user does nothing say for 1 or 2 seconds.

What is the best, not resource consuming, way to implement in WP8? Perhaps, the platform provides us with a useful service for that?

3

There are 3 answers

4
Amit Bhatiya On BEST ANSWER

I would expect an application that behaves in such a way to be loathed by the users of it. But if you must then you simply need to reset a timer every time something that you consider as "activity" within your app happens. Presumably this'll be some form of user interaction such as page navigation or interacting/tapping/editing content.

It's crude but it'll work. It'll also negatively impact the battery too. You have been warned.

For Other Alternative see Resetting Idle Detection Countdown in Windows Phone

0
berXpert On

Your case sounds like a good candidate for Reactive Extensions

The following example uses an observer of ListBox selection changed event, and then wait for 1.5 seconds until reporting an observation:

var itemsChanged = (from evt in Observable.FromEventPattern<SelectionChangedEventArgs>(MyList, "SelectionChanged")
                    select MyList.SelectedItems)
                   .Throttle(TimeSpan.FromSeconds(1.5));

itemsChanged.ObserveOnDispatcher()
            .Subscribe(items =>
                {
                    Debug.WriteLine("----------------");
                    foreach (var item in items)
                    {
                        Debug.WriteLine(item);
                    }
                }
        );
0
Muhammad Saifullah On

Here is one solution in your scenario. note it is just the pseudo code to convey my solution

private bool isLoadingData = false;
private bool newQueryQueued = false;

///code in your itemselectionchanged event handler
if(isLoadingData)
{
    newQueryQueued = true;
    //Do not execute your query.

}
else
{
    //Execute your query and get the result.
    //after the query result is completed, execute the following code.
    isLoadingData = false;
    if(newQueryQueued)
    {
        isLoadingData = true;
        newQueryQueued = false;
        //Execute your query and get the result.
    }
}

in the above pseudo code the query is executed only when it is need. say if use has tapped 4 item during first execution. it will not execute resource queries, because the previous result is still pending. and once the result is received the code will see if the user has changed his selection or not if the selection is changed than execute the query again.

and for the second part

I need to run the query if the user does nothing say for 1 or 2 seconds.

you need to implement timer on page load for what ever the time you want(1 or 2 seconds) and dispose off the timer after its first time elapses.

Hope this helps