.NET线程不是分开的吗? [英] .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)
当我在短视频(少于10秒)中运行此程序时,它会按预期使应用程序休眠20秒.但是,在浏览器的另一个选项卡上,我在一个长视频(大于10秒)上运行它,而第一个线程仍在睡眠中……但是它也睡眠了,直到20秒过期/过去了.因此,两个选项卡都处于睡眠状态,这向我表明它们确实是同一线程.使用Async/Await对时,VB.NET是否不应该创建单独的线程吗?
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.
推荐答案
异步和等待模式不是多线程的.使用相同的线程,但是以某种方式调度了该线程中的进程,以使该线程不会被锁定.在VS中,您可以检查调试期间正在运行的线程.
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.
这篇关于.NET线程不是分开的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!