如何访问WebClient.DownloadDataAsync获取的数据? [英] How to access data obtained by WebClient.DownloadDataAsync?
问题描述
(这是的后续问题>这个.)
我想异步读取无尽的音频流,以便对获得的数据进行JIT分析.使用WebClient
对象的DownloadDataAsync
方法,我可以轻松启动下载,请参见以下Win窗体项目:
I want to read an endless audio stream asynchronously, so that I can do my JIT analysis on the obtained data. Using a WebClient
object's DownloadDataAsync
method, I can initiate the download easily, see following Win form project:
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
事件输出到立即窗口,流似乎在流动:
and the stream seems to flow, as per the DownloadProgressChanged
event's output to the immediate 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
将永远不会触发(除了使用的事件之外,不会发生任何其他事件).
However, I lack to find a method to also read the obtained data. As this is an endless stream, DownloadDataCompleted
will never fire (nor does any other event except the one used).
如何访问获得的数据?
推荐答案
将OpenReadAsync
与OpenReadCompleted
事件结合使用.
Private Sub c_OpenReadCompleted(sender As Object, e As OpenReadCompletedEventArgs) Handles c.OpenReadCompleted
Dim stream = e.Result
End Sub
这篇关于如何访问WebClient.DownloadDataAsync获取的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!