|
-
Mar 6th, 2015, 12:50 PM
#1
Thread Starter
Frenzied Member
[RESOLVED] How to wait for an await method to cancel?
Hi!
I have an application where the user can scroll through different menu options (like a carousel). And for each of these menu options, different data is to be loaded in a content view.
If the user scrolls really slow, and wait at each carousel option, there is a text says "loading data..." and when the data has been loaded the info is displayed, nice and easy.
The code for this is quite easy, in pseudo it looks like inside the "carousel_changed" eventhandler
Data.Clear() // This clears the result ObservableCollection
hide result grid
show loading data...
start async call like
res = await LoadData(carouselId)
BindData(res)
BindData is a simple method that is adding the results to the list called Data.
The problem is when the user scrolls really fast through the carousel. Then all wierd stuff starts to happen in the UI, it becomes out of sync and the data is getting mixed up.
I tried implementing a solution with CancellationToken that is like this inside the "carousel_changed" event handler
check if data is loading by checking an IsLoading boolean
if data is loading, set the cancellationTolenSource.Cancel()
catch OperationCancelledException and reset the token.
The problem with the above is that there is time passing between when I call the Cancel() method on the tokenSource to when the actual Exception is caught which would indicate that the async operation was cancelled. This is due to the fact that I am calling multiple REST services and one of them might take 1-2 seconds to finish. And after each call I check
await call1
if(token.IsCAncellationRequested) then
token.ThrowIfCancellationRequested()
end if
await call2
if(token.IsCAncellationRequested) then
token.ThrowIfCancellationRequested()
end if
await call3
etc..
Maybe there is a better way? Lots of code...
And by that time, the await LoadData() method is called once again and aborted because the token is still in "Cancelled" state... and this just messes up things further.
I want some way to wait for the cancel operation to abort without locking up the UI thread. And also in the "carousel_changed" wait say 400 ms before actually start loading stuff, to prevent too many operations as a result of the user quickly scrolling through the carousel.
I have spent hours and hours of googling, and haven't found anything that would work.
Please help me get in sync!!! Any small example of code that would solve my scenario would be extremly useful.
/Henrik
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|