We help IT Professionals succeed at work.

multi thread event to form

Mutsop
Mutsop asked
on
Hi,

Using the TAPI library I found out it uses another thread.
I myself am not really an expert in multithreading and actually never used it. Seeing the articles on the net makes it even more complicated.

This is what I have so far:
Private Sub CallEvent(ByVal tapiEvent As TAPI3Lib.TAPI_EVENT, ByVal pEvent As Object) Handles TapiEvent.Event
        Dim thAsyncCall As System.Threading.Thread
        Dim infoChange As TAPI3Lib.ITCallStateEvent
        Select Case tapiEvent
            Case TAPI3Lib.TAPI_EVENT.TE_CALLNOTIFICATION 'Call Notification Arrived 
                ' assigning our sub's delegate to the thread 
                thAsyncCall = New Threading.Thread(AddressOf CallNotificationEvent)
                ' passing the variable for the thread 
                CallNotificationObject = CType(pEvent, TAPI3Lib.ITCallNotificationEvent)
                ' starting the thread 
                thAsyncCall.Start()
            Case TAPI3Lib.TAPI_EVENT.TE_CALLSTATE 'Call State Changes 
                ' assigning our sub's delegate to the thread 
                thAsyncCall = New Threading.Thread(AddressOf CallStateEvent)
                ' passing the variable for the thread 
                CallStateObject = CType(pEvent, TAPI3Lib.ITCallStateEvent)
                ' starting the thread
                thAsyncCall.Start()
            Case TAPI3Lib.TAPI_EVENT.TE_CALLINFOCHANGE 'Call Info Changes 
                ' assigning our sub's delegate to the thread 
                thAsyncCall = New Threading.Thread(AddressOf CallInfoEvent)
                ' passing the variable for the thread 
                CallInfoObject = CType(pEvent, TAPI3Lib.ITCallInfoChangeEvent)
                ' starting the thread 
                thAsyncCall.Start()
        End Select

        'Dim infoChange As TAPI3Lib.ITCallStateEvent
        'If tapiEvent = TAPI3Lib.TAPI_EVENT.TE_CALLSTATE Then
        '    infoChange = CType(pEvent, TAPI3Lib.ITCallStateEvent)
        '    callInfo = infoChange.Call
        '    RaiseEvent GetCallingNumber(callInfo.CallInfoString(TAPI3Lib.CALLINFO_STRING.CIS_CALLERIDNUMBER))
        'End If
    End Sub

Private CallNotificationObject As TAPI3Lib.ITCallNotificationEvent
    Private Sub CallNotificationEvent()
        ' here we should check to see various notifications of new and ended calls 
        Select Case CallNotificationObject.Event

            Case TAPI3Lib.CALL_NOTIFICATION_EVENT.CNE_MONITOR
                ' the notification is for a monitored call 
                RaiseEvent UpdateInfo("the notification is for a monitored call")
            Case TAPI3Lib.CALL_NOTIFICATION_EVENT.CNE_OWNER
                ' the notification is for an owned call 
                RaiseEvent UpdateInfo("the notification is for an owned call")
        End Select
    End Sub

Open in new window


and in my form:
Private Sub UpdateInfo(ByVal description As String) Handles Tapi.UpdateInfo
        TextBoxInfo.AppendText(description & Environment.NewLine)
    End Sub

Open in new window


Offcouse I get the cross threading error.
What should I change in the UpdateInfo from the form?
Comment
Watch Question

High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009
Commented:
Create a delegate and call your method recursively using Invoke():
Private Delegate Sub UpdateDelegate(ByVal description As String)

    Private Sub UpdateInfo(ByVal description As String) Handles Tapi.UpdateInfo
        If Me.InvokeRequired Then
            Me.Invoke(New UpdateDelegate(AddressOf UpdateInfo), New Object() { description })
        Else
            TextBoxInfo.AppendText(description & Environment.NewLine)
        End If
    End Sub

Open in new window

Author

Commented:
Sorry for late response but it works great, thanks.