Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Using Async/Await pattern for updating ProgressBar

$
0
0

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.




Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>