locked
Using Async/Await pattern for updating ProgressBar RRS feed

  • Question

  • So, now we have ability to create asynchronous code with async/await keywords.

    What I am after is a way to leverage this pattern for updating ProgressBar. After some trials I have come up with the following code:

        Private Async Sub AdvanceProgress() Handles btnAdvanceProgress.Click
    
            Dim max = 1000000
            pb.Minimum = 0
            pb.Maximum = max
    
            Await Task.Run(
                Async Function()
                    Dim j = 0
                    For i = 0 To max
                        j = i
                        If j Mod 2000 = 0 Then
                            Await Dispatcher.InvokeAsync(Sub() pb.Value = j)
                        End If
                    Next
                End Function).ConfigureAwait(False)
    
    End Sub

    Here are several things to notice:

    1) Using InvokeAsync instead of Invoke.

    2) Using ConfigureAwait with False.

    After running this code, all went as expected.

    Is this a right way to implement async/await pattern for updating ProgressBar? Is there some other way to implement this?



    There is no knowledge that is not power.



    • Edited by JohnyL Wednesday, July 17, 2013 10:58 AM Text corrected
    Wednesday, July 17, 2013 10:55 AM

Answers

  • 1) I don't think there's a real need for InvokeAsync here. Normally the worker thread couldn't care less if the progress bar has updated yet or not. BeginInvoke would do this job just as well.

    2). I'm not sure what do you expect that to do. In fact it's quite wrong to use that in this context.

    • Marked as answer by Lisa Zhu Saturday, July 27, 2013 7:13 AM
    Wednesday, July 17, 2013 11:20 AM

All replies

  • 1) I don't think there's a real need for InvokeAsync here. Normally the worker thread couldn't care less if the progress bar has updated yet or not. BeginInvoke would do this job just as well.

    2). I'm not sure what do you expect that to do. In fact it's quite wrong to use that in this context.

    • Marked as answer by Lisa Zhu Saturday, July 27, 2013 7:13 AM
    Wednesday, July 17, 2013 11:20 AM
  • Hi JohnyL,

    Since we haven't heard from you for a long time, I assumed the issue is resolved.I mark useful replies as answers. If you have any concerns, please feel free to reopen it or submit a new question. Thanks for your understanding.

    Regards,


    Lisa Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Saturday, July 27, 2013 7:13 AM