vb.net invoke method not working as expected.

Hi I have a zebra scanner and have code that executes on the barcode event.

Problem is, its on a different thread.
If i use invoke it says the mainform does not have a handle. Yet if i just all the function it works but does not see any data in the dataset on the mainform nor does it update the data on the UI but does in the sql server.

Please help.
LVL 3
taz8020Asked:
Who is Participating?
 
taz8020Author Commented:
OK done it you have to send mainform as parameter to Module.
0
 
ste5anSenior DeveloperCommented:
How should we know, where you're exactly got stuck??

crystal_ball_small.jpg
0
 
taz8020Author Commented:
?
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
ste5anSenior DeveloperCommented:
Code problems without posting any code.. How should we know where your problems are..
0
 
taz8020Author Commented:
On Mainform.vb.net I have code like:
Public Sub SetNewBarcode(barCodeEntered As String, ScannerSerialNumber As String)
        If Not Me.IsHandleCreated Then
            Me.CreateControl()
        End If
        Me.BeginInvoke(Sub() NewBarcode(barCodeEntered, ScannerSerialNumber))
    End Sub

    Public Sub NewBarcode(barCodeEntered As String, ScannerSerialNumber As String)
'Do Stuff here that gets data on mainform.vb dataset and updates ui
    End Sub

Open in new window


on Scanner Module:

Imports CoreScanner

Module ScannerCode
Private Sub OnBarcodeEvent(eventType As Short, ByRef scanData As String, ByRef SerialNumber As String)
        Try
           
            MainForm.NewBarcode(scanData , SerialNumber)
            'MainForm.SetNewBarcode(scanData , SerialNumber) ' does not work says no handle

        Catch generatedExceptionName As Exception
        End Try
    End Sub
End Module

Open in new window


Mainform.vb is the opening form set in vs. The Module is a code file i have created to catch the barcode event. If i call the function direct like  
MainForm.NewBarcode(scanData , SerialNumber) in the scanner module it works but does not see any data and does not update the ui.
If i use MainForm.SetNewBarcode(scanData , SerialNumber) get handle not created.
0
 
ste5anSenior DeveloperCommented:
Yup, you need to call the invoke in the scanner thread.

Module ScannerCode
  Private Sub OnBarcodeEvent(eventType As Short, ByRef scanData As String, ByRef SerialNumber As String)
    Me.Invoke(Sub()
		MainForm.SetNewBarcode(scanData , SerialNumber)   
		)    
  End Sub
End Module

'with Main:
Public Sub SetNewBarcode(barCodeEntered As String, ScannerSerialNumber As String)
  If Not Me.IsHandleCreated Then
    Me.CreateControl()
  End If
  NewBarcode(barCodeEntered, ScannerSerialNumber)
End Sub

Open in new window


or

Module ScannerCode
  Private Sub OnBarcodeEvent(eventType As Short, ByRef scanData As String, ByRef SerialNumber As String)    
    MainForm.SetNewBarcode(scanData , SerialNumber)   	
  End Sub
End Module

'with Main:
Public Sub SetNewBarcode(barCodeEntered As String, ScannerSerialNumber As String)
  If Me.InvokeRequired Then 
    Me.Invoke(Sub() MainForm.SetNewBarcode(scanData , SerialNumber)) 
  Else 
    If Not Me.IsHandleCreated Then
      Me.CreateControl()
    End If
    NewBarcode(barCodeEntered, ScannerSerialNumber)
  End If 
End Sub

Open in new window


The problem is that the guard-if is not in your original invocation call. Thus it is failing.
0
 
taz8020Author Commented:
Hi thanks but still does not work. It get to SetNewBarcode in mainform does not require invoke, it does create control but it is already open (its like its creating another instance of it). An when its nets to newbarcode does not see data or update ui. It does save the data back to sql. I have added a timer on main form to refresh every 5 secs to test it. The timer works but not the newbarcode. Its like its created another instance of mainform.
0
 
ste5anSenior DeveloperCommented:
[..] control but it is already open [..]
What does this mean???

  If Not Me.IsHandleCreated Then
    Me.CreateControl()
  End If
  NewBarcode(barCodeEntered, ScannerSerialNumber)

Open in new window

Is just copied from your snippets. I have to admit, that I don't see the sense behind it, when capturing a barcode at all..

Maybe it's time for context..
0
 
taz8020Author Commented:
seen it on a site when searching for an answer.

if i use
  If Not Me.IsHandleCreated Then
    Me.CreateControl()
  End If

Open in new window

it makes no difference if its there or not. If it is there it does CreateControl but same result with or without
0
 
ste5anSenior DeveloperCommented:
cough, cough, "seen it on a site"

As I said, it makes no sense. Maybe time to drop it at all...
0
 
taz8020Author Commented:
I have taken it out but as i said does not make a difference, any advise.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Assuming you only have one instance, you can obtain a reference to the Mainform in your Module like this (without passing a reference):
Public Module ScannerCode

    Private Sub OnBarcodeEvent(ByVal eventType As Short, ByVal scanData As String, ByVal SerialNumber As String)
        Try
            Dim frm As Mainform = Application.OpenForms.OfType(Of Mainform).FirstOrDefault
            If Not IsNothing(frm) Then
                frm.Invoke(Sub()
                               frm.NewBarcode(scanData, SerialNumber)
                           End Sub)
            End If
        Catch generatedExceptionName As Exception
        End Try
    End Sub

End Module

Open in new window


Note that the parameters to OnBarcodeEvent() are all ByVal, not sure why you have ByRef in there.
1
 
taz8020Author Commented:
found it on stack overflow
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.