I've read a lot of tutorials with regards to await/async and i've understood that when it comes to await
keyword it will go back to the main context
then go back to await
(when it's finished) and then continue further if there's something after away
within async
method. I also read that there is something like ConfigureAwait(False)
which simply means that with opposite to i've written before when it comes to await
it will not go back to main context
at this point but will stay here and wll wait on await
to be finished and then continue in async
method after that will go back to main cotext
. So diffrence is it will not go back to main context
but wait on await
. Correct if i am wrong. Based on that knowledge in first example i should see following result:
After DoStafAsync
Done running the long operation asynchronously.
but when i just simply add ConfigureAwait(False) it should be:
Done running the long operation asynchronously.
After DoStafAsync
as it will not go back to context but will wait and after async method finishes then will go back. Somehow i see the result as in first output. What i am missing?
Public Class Form1
Sub LongOperation()
Threading.Thread.Sleep(5000)
End Sub
Private Async Function DoStafAsync() As Task
lblStatus.Text = "Running a long operation asynchronously... (UI thread should be fully responsive)"
Await Task.Run(Sub()
LongOperation()
End Sub).ConfigureAwait(False)
lblStatus.Text = "Done running the long operation asynchronously."
End Function
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
dfg
lblStatus.Text = "After DoStafAsync"
End Sub
Async Sub Dfg
Await DoStafAsync
End Sub
End Class
Additional question: Some people claims that tasks are not creating threads but working on same ui thread. But there are some people claiming that they are and there are also people claiming that sometimes tasks creating threads and sometimes not. What is true here?