In researching the new .NET Async and Await keywords in .NET 4.5, I created a simple example showing how to update a progress bar and a label while an async function is running. I also included how to cancel the process while it is taking place.
To use the code below, create a Windows form and include two buttons:
Button1
= start buttonButton2
= stop button
Also add a progress bar called ProgressBar1
and a label called Label1
.
A few things to note:
The functions use ASYNC and AWAIT "all the way down" from the initial button click to the final VB function that supports asynchronous support;
Note the use of the
cancellationtokensource
to signal that we want to cancel things;The final asynchronous VB function we use is
WebClient.DownloadStringTaskAsync()
. This is arbitrary, and is used for example purposes only.
Please let me know if you have any questions or problems with this code.
' Simple progress bar example with cancellation and label updating
' Using the ASYNC and AWAIT keywords available through .Net 4.5
' Helpful sources include:
' - http://msdn.microsoft.com/en-us/library/hh191443.aspx
' - http://blogs.msdn.com/b/dotnet/archive/2012/06/06/async-in-4-5-enabling-progress-and-cancellation-in-async-apis.aspx
Imports System.Net
Imports System.Threading
Public Class Form1
Dim test As WebClient = New WebClient ' This is specific to the type of async function we're doing. Yours may be different.
Dim myToken As CancellationTokenSource ' This is used to notify the async function that we want to cancel it.
Dim myProgress As Progress(Of Integer) ' We use this as the vehicle for reporting progress back to our GUI thread
Dim Counter As Integer = 0 ' This is a variable that we use to track our progress (specific to this example)
Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ProgressBar1.Value = 0
Counter = 0
myToken = New CancellationTokenSource
Await startProgressBar(myToken) ' Note that the signature of our startprogressbar method includes the cancellationtokensource
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
If Not myToken Is Nothing Then
myToken.Cancel() ' We tell our token to cancel
End If
End Sub
Public Async Function startProgressBar(ct As CancellationTokenSource) As Task
Dim progressTarget As Action(Of Integer) ' We have to create an event handler for the function we use to report progress
progressTarget = AddressOf ReportProgress ' We assign the handler to our specific function (see below)
myProgress = New Progress(Of Integer)(progressTarget) ' When we initialize our progress reporter, we pass our handler to it
For I = 0 To 9
If ct.IsCancellationRequested = True Then
Exit For ' We then break out of the loop
End If
Dim myString As String = Await RetrieveString(myProgress) ' We don't do anything with this string, just so we can run our async function
Next
End Function
Public Async Function RetrieveString(ByVal myProgress As IProgress(Of Integer)) As Task(Of String)
Dim myString As String = ""
If Not myProgress Is Nothing Then 'If we have a valid progress object, report our progress using it
Counter = Counter + 10
myProgress.Report(Counter)
End If
myString = Await test.DownloadStringTaskAsync("http://www.cnn.com") ' This is a throwaway function - just something that uses async
Return myString ' We are really not doing anything with the results of this function, just returning it
End Function
Private Sub ReportProgress(ByVal myInt As Integer)
Label1.Text = "Step Number: " & myInt.ToString
ProgressBar1.Value = myInt
End Sub
End Class
Button1
asStartButton
andButton2
asStopButton
? \$\endgroup\$