0

(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 Answers1

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
Magnus
  • 45,362
  • 8
  • 80
  • 118
  • Ok, so `OpenReadCompleted` fires. I am not sure what to do next in the event. All the processing on the fly, reading byte for byte, surely would not be wise, as this event ought to run in an own thread, thence likely running into issues when manipulating variables of a wider scope, no? - However, using Read rather than ReadByte and storing the data class-wide, then leaving the event does not seem to work: the event does not fire again to provide the next stream portion. - I guess I simply don't understand the concept. –  Jul 07 '17 at 08:51
  • This event gives you the stream once the WebClient has connected to the remote server. It is up to you to read from it. Usually one would read a chunk into a buffer, process that and continue with the next chunk. But I have no idea what you want to do with the data. – Magnus Jul 07 '17 at 09:03
  • @Herb : Streams support asynchronous reading/writing as well. – Visual Vincent Jul 07 '17 at 09:10
  • @Magnus How would you stop the downloading as this is continuous? – alwaysVBNET Jul 07 '17 at 12:59
  • @Magnus from the code above, the OpenReadCompleted never fires – alwaysVBNET Jul 07 '17 at 13:03
  • @alwaysVBNET You stop when you want to:. Exist program, click a button... If it doesn't fire maybe webClient cant connect to remote. – Magnus Jul 07 '17 at 14:04
  • @Magnus any ideas why wouldn't something like client.CancelAsync() work? – alwaysVBNET Jul 07 '17 at 16:31