Abort async operation when changing pivot

A place for App developers to hang out / post
Post Reply
MrNorth

Posts: 60
Joined: Sat May 18, 2013 9:46 am
Location:

HTPC Specs: Show details

Abort async operation when changing pivot

#1

Post by MrNorth » Wed Jan 29, 2014 2:45 pm

Hi!

I am having an interesting problem.

My app is a rather standard gallery with a pivot for selecting category. When I select category 1, I set off an async load to get items for the gallery repeater. If I then, before the load is complete, browse to category 2 which takes very short time to load, what happens is that in the gallery, first the items for category 2 is displayed, and then after a few seconds when category 1 items are loaded, the items in the gallery are replaced with category 1 items. Is there any way to abort the async process (if one is running) when changing categories? So if I have a load in progress, kill it and do a new load. Like a singleton task factory...

The code I use to start loading is:

Code: Select all

Microsoft.MediaCenter.UI.Application.DeferredInvokeOnWorkerThread(doLoad, onLoad, null);

MrNorth

Posts: 60
Joined: Sat May 18, 2013 9:46 am
Location:

HTPC Specs: Show details

#2

Post by MrNorth » Sun Feb 02, 2014 1:20 pm

I figured I bump this, since I really need a good solution for this. And I don't think I am the first one that has stumbled upon it.

What I am curious about is that if there is a good way to handle this with the media center classes and methods available for invoking async delegates, or if I should forget about them all together and use something like abackgorundworker instead? Aborting async delegates is not straightforward, and you need to pass along a cancellationtoken or something to abort an async operation. Surely there must be a best practice method for doing this stuff with media center development? It is handled nicely in the netflix app, where the loading aborts when you shift pivots.

cheers
H

MrNorth

Posts: 60
Joined: Sat May 18, 2013 9:46 am
Location:

HTPC Specs: Show details

#3

Post by MrNorth » Mon Feb 03, 2014 8:06 pm

Hi!

I spent some hours writing a wrapper of sorts using the BackgroundWorker class. It worked OK, I got the cancellation stuff working, but I had to be careful when updating databound items, such as ArrayListDataSet collections, from the WorkCompleted delegate, I got all kinds of wierd problems with the UI. It was solved my creating a temp collection that was used to parse the result from the async operation, and then safely outside the BackgroundWorker, the databound collection was updated.

The code is not pretty, but it did the trick. When time permits I will make a more elegant solution without the BGW class, dependig on the framework version I can use...

case closed!

/H

Post Reply