.NET Threads are not separate?

 ̄綄美尐妖づ 提交于 2019-12-11 18:16:07

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/51400594/net-threads-are-not-separate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!