如何访问WebClient.DownloadDataAsync获取的数据?
How to access data obtained by WebClient.DownloadDataAsync?
(这是 的后续问题。)
我想异步读取无穷无尽的音频流,以便对获取的数据进行 JIT 分析。使用 WebClient
对象的 DownloadDataAsync
方法,我可以轻松启动下载,请参阅以下 Win 窗体项目:
Imports System.Net 'For WebClient
Public Class Form1
Private WithEvents c As New WebClient()
Protected Overrides Sub Finalize()
c.Dispose()
MyBase.Finalize()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim u As New Uri("http://flower.serverhostingcenter.com:8433/;")
c.DownloadDataAsync(u)
End Sub
Private Sub c_DownloadProgressChanged(sender As Object, e As DownloadProgressChangedEventArgs) Handles c.DownloadProgressChanged
Debug.Print(e.BytesReceived.ToString & " B received.")
End Sub
End Class
并且流似乎在流动,根据 DownloadProgressChanged
事件的输出到即时 window:
2920 B received.
48180 B received.
56940 B received.
61320 B received.
87600 B received.
94900 B received.
160436 B received.
162060 B received.
227596 B received.
...
但是,我找不到一种方法来也读取 获得的数据。由于这是一个无穷无尽的流,DownloadDataCompleted
将永远不会触发(除了使用的事件之外,任何其他事件也不会触发)。
如何访问获取的数据?
将 OpenReadAsync
与 OpenReadCompleted
事件结合使用。
Private Sub c_OpenReadCompleted(sender As Object, e As OpenReadCompletedEventArgs) Handles c.OpenReadCompleted
Dim stream = e.Result
End Sub
(这是
我想异步读取无穷无尽的音频流,以便对获取的数据进行 JIT 分析。使用 WebClient
对象的 DownloadDataAsync
方法,我可以轻松启动下载,请参阅以下 Win 窗体项目:
Imports System.Net 'For WebClient
Public Class Form1
Private WithEvents c As New WebClient()
Protected Overrides Sub Finalize()
c.Dispose()
MyBase.Finalize()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim u As New Uri("http://flower.serverhostingcenter.com:8433/;")
c.DownloadDataAsync(u)
End Sub
Private Sub c_DownloadProgressChanged(sender As Object, e As DownloadProgressChangedEventArgs) Handles c.DownloadProgressChanged
Debug.Print(e.BytesReceived.ToString & " B received.")
End Sub
End Class
并且流似乎在流动,根据 DownloadProgressChanged
事件的输出到即时 window:
2920 B received.
48180 B received.
56940 B received.
61320 B received.
87600 B received.
94900 B received.
160436 B received.
162060 B received.
227596 B received.
...
但是,我找不到一种方法来也读取 获得的数据。由于这是一个无穷无尽的流,DownloadDataCompleted
将永远不会触发(除了使用的事件之外,任何其他事件也不会触发)。
如何访问获取的数据?
将 OpenReadAsync
与 OpenReadCompleted
事件结合使用。
Private Sub c_OpenReadCompleted(sender As Object, e As OpenReadCompletedEventArgs) Handles c.OpenReadCompleted
Dim stream = e.Result
End Sub