0

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.

swabygw
  • 813
  • 1
  • 10
  • 22
  • 2
    Try doing `Await Task.Delay(TimeSpan.FromSeconds(20.0))` instead of `Thread.Sleep(20000)`. – Enigmativity Jul 18 '18 at 11:22
  • So this is ASP.Net? If so and your using session state then requests within the same session will be serialized - i.e the request from the 2nd tab is queued by IIS until the 1st completes. – Alex K. Jul 18 '18 at 11:23
  • Thnx. How can I get it to create separate threads, then? (the idea was to have them run separately, so multiple users can get duration) – swabygw Jul 18 '18 at 11:23
  • Multiple users will be in different sessions so this should not be an issue. To test instead of using a 2nd tab use a new Private Browsing window or different browser & see if the same thing happens. – Alex K. Jul 18 '18 at 11:27
  • Take a look at this question: [If async-await doesn't create any additional threads, then how does it make applications responsive?](https://stackoverflow.com/questions/37419572/if-async-await-doesnt-create-any-additional-threads-then-how-does-it-make-appl) – JayV Jul 18 '18 at 11:29
  • @AlexK.: you were right. I tested it as you suggested. I can get this done without threading at all since they're in different sessions. – swabygw Jul 18 '18 at 11:51

1 Answers1

0

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.

cy-c
  • 855
  • 8
  • 8
  • Thnx. How can I get it to create separate threads, then? (the idea was to have them run separately, so multiple users can get duration) – swabygw Jul 18 '18 at 11:22
  • I'm no expert at VB, but .net supports parallel operations. There should be a VB equivalent: https://learn.microsoft.com/en-us/dotnet/standard/parallel-programming/how-to-use-parallel-invoke-to-execute-parallel-operations – cy-c Jul 18 '18 at 11:29