How to access data obtained by WebClient.DownloadDataAsync?

[亡魂溺海] 提交于 2019-12-12 03:27:25

问题


(This is a follow-up question to this one.)

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

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.
...

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).

How do I access the obtained data?


回答1:


Use OpenReadAsync in conjunction with the OpenReadCompleted event.

Private Sub c_OpenReadCompleted(sender As Object, e As OpenReadCompletedEventArgs) Handles c.OpenReadCompleted
   Dim stream = e.Result
End Sub


来源:https://stackoverflow.com/questions/44964227/how-to-access-data-obtained-by-webclient-downloaddataasync

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