Click here to Skip to main content
15,888,330 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have async await method in my form that I called when button1 is clicked. How do I stop the async await when I click button2?

What I have tried:

VB
Private Async Sub Flash()
	While True
		Await Task.Delay(100)
		Label1.Visible = Not Label1.Visible
	End While
End Sub


Above code is my Async Await method
Posted
Updated 23-Mar-17 4:01am
Comments
Ralf Meier 23-Mar-17 7:41am    
What do you try to achieve ...?
I think (but I'm not sure) that your Approach is wrong ...
dell-gl62m 23-Mar-17 7:45am    
I am making something like alarm where when the time is up the label will keep blinking and stop the blink when button2 is clicked.
dell-gl62m 23-Mar-17 7:49am    
I am referring to this link http://stackoverflow.com/questions/42970996/how-to-make-a-label-blink and do something like this.
Ralf Meier 23-Mar-17 7:59am    
So you have your own (ciústomized control / Label) ...
What about using (for example) the instance of a Timer in your control. Here you could do all you like inside the method which handels Timer.Tick AND the Timer could be stopped at each time you want ...
Dave Kreskowiak 23-Mar-17 9:00am    
How do you stop an "Async Await"? You don't. You're not understanding the concepts of async and await.

You should be using a Timer, not a Task, for this.

1 solution

Use a CancellationTokenSource[^] and a CancellationToken[^]:
VB.NET
Private _cts As CancellationTokenSource

Private Async Function Flash(ByVal token As CancellationToken) As Task
    While Not token.IsCancellationRequested
        Await Task.Delay(100, token)
        Label1.Visible = Not Label1.Visible
    End While
End Function

Private Async Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim newSource As New CancellationTokenSource()
    Dim oldSource As CancellationTokenSource = Interlocked.Exchange(_cts, newSource)
    If oldSource IsNot Nothing Then oldSource.Cancel()
    
    button1.Enabled = False
    button2.Enabled = True
    Try
        Await Flash(newSource.Token)
    Finally
        Interlocked.Exchange(_cts, Nothing)
        button2.Enabled = False
        button1.Enabled = True
    End Try
End Sub

Private Sub button2_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim cts As CancellationTokenSource = Interlocked.Exchange(_cts, Nothing)
    If cts IsNot Nothing Then cts.Cancel()
End Sub
 
Share this answer
 
v2
Comments
Dave Kreskowiak 23-Mar-17 10:50am    
That's a sh*t-ton of code to make a label blink. :)
Richard Deeming 23-Mar-17 10:57am    
I was treating it as a general-purpose "how to stop async/await", rather than "what's the best way to make a label blink". :)
Dave Kreskowiak 23-Mar-17 11:09am    
You probably made his head explode too. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900