I'm using a 3rd party .NET library which monitors a peripheral with a backgroundworke r. I've added a handler to catch a raised event, but when I try to update the UI I receive an exception because my UI call is on the raised event thread from the background worker and not the UI thread. The 3rd party library states that UI events should be called with ControlBeginInv oke, but I've had no luck trying to get it to work. The program is supposed to be very simply; so, I'd like to avoid a lot of plumbing if possible. Here's what I have so far. I'm not including all of my attempts to call the UI thread.
[code=vbnet]
Public Class frmMain
Dim m_ctmPeripheral Manager As PeripheralDevic eManager = PeripheralDevic eManager.Single ton
Private Sub fmrMain_Load(By Val sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Load
' Start the peripheral Manager
m_ctmPeripheral Manager.Startup (True)
' Attach the event handlers
AddHandler m_ctmPeripheral Manager.Periphe ralActivatedEve nt, AddressOf testEvent
AddHandler m_ctmPeripheral Manager.Periphe ralDeActivatedE vent, AddressOf testEvent2
End Sub
Private Sub testEvent(ByVal sender As Object, ByVal args As PeripheralDevic eEventArgs)
' Update the UI TextBox == This fails b/c the UI thread doesn't call this function
txtOutput.Text = txtOutput.Text & vbCrLf & ("Got " & args.Peripheral Device.Internal Name & ", on slot " & args.Slot.ToStr ing)
End Sub
Private Sub testEvent2(ByVa l sender As Object, ByVal args As PeripheralDevic eEventArgs)
' Update the UI TextBox == This fails b/c the UI thread doesn't call this function
txtOutput.Text = txtOutput.Text & vbCrLf & ("Got " & args.Peripheral Device.Internal Name & ", on slot " & args.Slot.ToStr ing)
End Sub
End Class[/code]
TIA.
[code=vbnet]
Public Class frmMain
Dim m_ctmPeripheral Manager As PeripheralDevic eManager = PeripheralDevic eManager.Single ton
Private Sub fmrMain_Load(By Val sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Load
' Start the peripheral Manager
m_ctmPeripheral Manager.Startup (True)
' Attach the event handlers
AddHandler m_ctmPeripheral Manager.Periphe ralActivatedEve nt, AddressOf testEvent
AddHandler m_ctmPeripheral Manager.Periphe ralDeActivatedE vent, AddressOf testEvent2
End Sub
Private Sub testEvent(ByVal sender As Object, ByVal args As PeripheralDevic eEventArgs)
' Update the UI TextBox == This fails b/c the UI thread doesn't call this function
txtOutput.Text = txtOutput.Text & vbCrLf & ("Got " & args.Peripheral Device.Internal Name & ", on slot " & args.Slot.ToStr ing)
End Sub
Private Sub testEvent2(ByVa l sender As Object, ByVal args As PeripheralDevic eEventArgs)
' Update the UI TextBox == This fails b/c the UI thread doesn't call this function
txtOutput.Text = txtOutput.Text & vbCrLf & ("Got " & args.Peripheral Device.Internal Name & ", on slot " & args.Slot.ToStr ing)
End Sub
End Class[/code]
TIA.
Comment