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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
How should we know, where you're exactly got stuck??

crystal_ball_small.jpg
0
taz8020Author Commented:
?
0
ste5anSenior DeveloperCommented:
Code problems without posting any code.. How should we know where your problems are..
0
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

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
taz8020Author Commented:
OK done it you have to send mainform as parameter to Module.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.