Posts: 2319
Joined: Mon Jan 09, 2006 2:20 pm
Location: Sweden

Getting a stream asynchronously (VB.NET)

Tue Jun 13, 2006 2:04 pm

Until BlueTools natively allows for an asynchronous call to RemoteService.Stream this class below can be used as a work-around.

It used like this:

Code: Select all

// Where you usually try to get the Stream synchronously.
Dim async As StreamAsync
async = New StreamAsync(m_serviceCurrent, AddressOf streamreceived)
async.Parent = Me
The streamreceived method will then be called when the RemoteService.Stream returns a Stream.

The method should look like this:

Code: Select all

Private Sub streamreceived(ByVal sender As System.Object, ByVal stream As Franson.BlueTools.ServiceStream)

' do what you want with the Stream here

End Sub
Below is the class (just copy and paste into a file):

' (c) 2006 Franson Technology AB, Sweden, All rights reserved
' Written by Jonas Sandman
' This code is provided merely as an example.
' No guarantees are made for its functionality or stability.

Code: Select all

Public Class StreamAsync

    ' reference to Service we want a Stream to
    Dim m_service As Franson.BlueTools.RemoteService = Nothing
    Dim m_callback As StreamAsyncReceived = Nothing
    Dim m_control As System.Windows.Forms.Control = Nothing

    ' delegate to call when Stream is received
    Public Delegate Sub StreamAsyncReceived(ByVal sender As Object, ByVal stream As Franson.BlueTools.ServiceStream)

    ' Constructor
    ' Since Visual Basic does not support dynamic events, we pass the callback method in the constructor
    Public Sub New(ByVal service As Franson.BlueTools.RemoteService, ByVal callback As StreamAsyncReceived)

        m_service = service
        m_callback = callback

    End Sub

    ' call this method to actually retrieve stream
    Public Sub GetStreamAsync()

        ' run threadpool
        System.Threading.ThreadPool.QueueUserWorkItem(New System.Threading.WaitCallback(AddressOf Run))

    End Sub

    Public Property Parent() As System.Windows.Forms.Control


            Return m_control

        End Get

        Set(ByVal Value As System.Windows.Forms.Control)

            m_control = Value

        End Set
    End Property

    Private Sub Run(ByVal obj As Object)

        Dim stream As Franson.BlueTools.ServiceStream

        ' get the stream - this is in a thread!
        stream = m_service.Stream

        If (Parent Is Nothing) Then
            m_callback(Me, stream)
            Parent.Invoke(m_callback, New Object() {Me, stream})

        End If

    End Sub

End Class
Franson Support

Return to “Knowledge base BlueTools”