|
-
Mar 8th, 2015, 01:54 PM
#3
Thread Starter
Frenzied Member
Re: How to wait for an await method to cancel?
 Originally Posted by IanRyder
Hi,
I have faced a similar problem in the past and I solved this by thinking about this a bit differently. I found that trying to wait for the cancellation of a background thread could take a variable amount of time depending on the data being loaded and I always ended up duplicating data and messing up the UI similar to what you are experiencing now.
To get round this, and to let the User scroll through options as fast as they wanted to, I decided that I needed to let the Background Thread finish its current work before trying to get it to do something else. The trick here was to make sure that the UI Thread continued to keep track of what the user was doing, doing minimal work on the UI Thread, and then only processing the LAST Selected Option once the Background Thread became available again. I achieved this by adding each selected user option to a Stack and then once the Background Thread became available the LAST item on the Stack was retrieved and processed on the background thread.
Here’s a quick example using a bunch of TextBox’s on a form. Just run the cursor over the TextBox’s as quick as you want and you will see that only the last textbox is processed when the Backgoundworker becomes free. Maybe you can adapt this idea to your Carousel problem.
vb.net Code:
Public Class Form1
Private carouselStack As New Stack(Of String)
Private Sub Button1_Enter(sender As Object, e As EventArgs) Handles Button1.MouseEnter, Button2.MouseEnter, _
Button3.MouseEnter, Button4.MouseEnter, Button5.MouseEnter, Button6.MouseEnter, Button7.MouseEnter
carouselStack.Push(sender.name)
If Not BackgroundWorker1.IsBusy Then
BackgroundWorker1.RunWorkerAsync()
End If
End Sub
Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
'do some labour intensive work
Dim lastCarouselSelection As String = carouselStack.Pop
carouselStack.Clear()
Label1.Invoke(Sub()
Label1.Text = String.Format("Processing Button {0}", lastCarouselSelection)
End Sub)
Threading.Thread.Sleep(2000)
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
If carouselStack.Count > 0 Then
BackgroundWorker1.RunWorkerAsync()
End If
End Sub
End Class
Hope that helps.
Cheers,
Ian
Thanks for the reply.
Yeah, I ended up doing something similar to your idea, but instead of queuing up all calls, in the carousel_changed event handler I check if work is in progress AND if there is a cancellation in progress. If that is the case I fire up a while(true) loop (with a 200 ms delay between each iteration)in a non UI thread that simply catches all calls, and I exit that loop only when the taskcancelledexception is fired meaning that the work has been terminated. Only then do I exit the while loop and keep doing the work. The "selectedIndex" property, which I use in the work method, is updated when the user scroll through the carousel since it is done on the UI thread, Everything is smooth as silk and I don't get any wierd binding problems.
It is funny, I thought there was a "by the book" way to solve this, but since we came up with two very different ways to solve it, by async/await + a while loop or in your case with a stack, I guess the Task-library has nothing ready for us. Using Task.Wait is quite useless since it is old style Task programming and it may cause deadlocks if not handled with special care in combination with .ContinueWith() which was very popular.
Carousels and pivots are aspecial case, normally in standard desktop apps I just disable the "work"-button and enables it once the task is cancelled. But in Modern UI and Phone 8.1 apps, the pivots and carousels call for special attention.
cheers!
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
|