Search code examples
vb.netmultithreadingasynchronousasync-awaitnreco

.NET Threads are not separate?


I have a sub which gets the duration of a video file.

Async Sub GetDuration(folder As String)

Dim ffP As New FFProbe
Dim vInfo As MediaInfo

Dim totalSecs As Double = Await Task.Run(Function()
                                           vInfo = ffP.GetMediaInfo(filep)
                                           If vInfo.Duration.TotalSeconds < 10
                                            Thread.Sleep(20000)
                                           End If
                                           Return vInfo.Duration.TotalSeconds
                                         End Function)

When I run this on a short video (less than 10 seconds), it sleeps the application for 20 seconds, as expected. On another tab in the browser, however, I run it on a long video (greater than 10 seconds), while the first thread is still sleeping...but it sleeps, too, until the 20 seconds has expired/passed. So both tabs are sleeping, which indicates to me that they are really the same thread. Isn't VB.NET supposed to create separate threads when using the Async/Await pair?

The main idea was to have them run separately, so multiple users can get duration independently of each other.


Solution

  • Async and await patterns are not multithreading. The same thread is used, but the processes in that thread are scheduled in a way so that the thread is not locked. In VS, you can inspect the threads you have running during debug.