This is for .NET Compact Framework 2.0
Here's the flow of what I'm trying to do:
1. On the main form a scanner class is instantiated that (in the scanner's constructor) sets the scanner settings (baud rate, parity, etc) and opens the scanner for use
2. A barcode is scanned (via a scanner connected to the device in a seral port) while on the main form
3. In the scanner class, the DataRecieved event gets the needed info from the scanned code.
4. Then that info is passed back to the main form to a method RunCommandCode (which takes the scanned info as an argument)
5. On the main form the RunCommandCode calls another method CommandCodeHandler, passing the scanned info again
6. In the CommandCodeHandler sub a method is called in a Utils class (SetSelectedTab), which interacts with the form itself (determines how many tabs exist on a tab control), which is the reason for the invoke in the first place
So, before I figured out I needed to use the Invoke method (and, more specifically, that the scanner was working in a different thread), I was getting an error in step 6 on the line that got the TabControl.TabPages.Count.
Here's the DataRecieved event handler in the scanner class:
As you can see in the code, I'm getting one of those insanely vague error messages that is completely useless (end of short rant) on the line in which I call the delegate.
Here's the delegate sub and the sub it's for:
I realized that all of the examples I was reading were for using Invoke on controls, when, in this case, I need to use it on a method. At least I'm guessing that's the reason for the error.
I'd appreciate it if anyone could provide some helpful information on this. This is my first foray into threads, delegates, and Invoke, so be gentle!
Thanks
Here's the flow of what I'm trying to do:
1. On the main form a scanner class is instantiated that (in the scanner's constructor) sets the scanner settings (baud rate, parity, etc) and opens the scanner for use
2. A barcode is scanned (via a scanner connected to the device in a seral port) while on the main form
3. In the scanner class, the DataRecieved event gets the needed info from the scanned code.
4. Then that info is passed back to the main form to a method RunCommandCode (which takes the scanned info as an argument)
5. On the main form the RunCommandCode calls another method CommandCodeHandler, passing the scanned info again
6. In the CommandCodeHandler sub a method is called in a Utils class (SetSelectedTab), which interacts with the form itself (determines how many tabs exist on a tab control), which is the reason for the invoke in the first place
So, before I figured out I needed to use the Invoke method (and, more specifically, that the scanner was working in a different thread), I was getting an error in step 6 on the line that got the TabControl.TabPages.Count.
Here's the DataRecieved event handler in the scanner class:
VB.NET:
Private Sub comScanner_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles comScanner.DataReceived
If comScanner.IsOpen Then
Dim sCode As String = comScanner.ReadExisting()
Dim asCodes() As String = sCode.Split(CChar(vbCrLf))
For i As Integer = 0 To UBound(asCodes)
If asCodes(i).Trim() <> "" Then
Dim objCC As New CommandCode()
objCC.CommandCode = asCodes(i)
Dim cc() As CommandCode = {objCC}
Dim frm As frmMain = CType(m_objCallForm, frmMain)
' Error "ArgumentException"
frm.Invoke(New frmMain.RunCommandCodeInvoker(AddressOf frm.RunCommandCode), cc)
End If
Next
End If
End Sub
As you can see in the code, I'm getting one of those insanely vague error messages that is completely useless (end of short rant) on the line in which I call the delegate.
Here's the delegate sub and the sub it's for:
VB.NET:
Public Delegate Sub RunCommandCodeInvoker(ByVal args() As Object)
Public Sub RunCommandCode(ByVal args() As Object)
MyCommandCodeSystem.CurrentHandler = AddressOf CommandCodeHook
ComScanner.CurrentForm = Me
CommandCodeHandler(args)
End Sub
I realized that all of the examples I was reading were for using Invoke on controls, when, in this case, I need to use it on a method. At least I'm guessing that's the reason for the error.
I'd appreciate it if anyone could provide some helpful information on this. This is my first foray into threads, delegates, and Invoke, so be gentle!
Thanks