I wrote an application in WPF / VB and separated the business logic and UI into different projects.
The business layer uses a serial port which runs on a different thread, Now that I'm trying to write a command line interface for the same business layer, it seems to fail when .Invoke() is called. (no error, just doesn't work)
I'm pretty sure the reason I had to add in checkaccess and .invoke was because I have collections that would be changed during processing the serial port data and wanted the NotifyCollectionChanged to be handled by WPF data binding. (The reason I'm not 100% sure is because it was months ago I wrote that part and it all worked great from the GUI, now adding the console app has made me rethink some of this)
I would like my business layer to run these processes on the thread they were created, I need this to work from both my GUI version and the command line version.
Am I misusing the Dispatcher in my business layer? Is there a better way to handle an event from the serial port, and then return to the main thread to processes the data?
Updated:
Private Delegate Sub NewDataRecieved(ByVal byteBuffer() As Byte)
Private Sub DataReceived(ByVal byteBuffer() As Byte) Handles _serial.DataRecieved
If _dispatcher.CheckAccess() Then
ProcessTheData
Else
Dim dataReceivedDelegate As NewDataRecieved
dataReceivedDelegate = New NewDataRecieved(AddressOf DataReceived)
_dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, dataReceivedDelegate, byteBuffer)
End If
End Sub