Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have read posts here and elsewhere online but cannot figure out what the problem is. Please help. FYI, I am new to OOP.
I have 2 (simplified)code files shown below, one with the UI form and one class for the serial port stuff. When the beginInvoke occurs I get the uncreated handle error. If I check for the handle in Form1 in the form load it of course exists and has a value.
If I take all the serPort_cls code and rearrange it so it is part of the Form1 class it works fine. I suspect I am missing something very obvious.

VB
#Region "formFile"
 Public Class Form1

    Dim sp As New serPort_cls

    Public Sub processRcvdData()
        'do something useful here, msgbox() just for testing
        MsgBox("data rcvd")
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim h As Integer
        If Me.IsHandleCreated Then
            h = Handle              ' yes, handle does exist, here it is
        End If
        'Me.CreateHandle()
        sp.openCom()

    End Sub

End Class
#End Region


#Region "classFile"
 Imports System.IO.Ports

Public Class serPort_cls

    Private WithEvents serPort As New SerialPort
    Delegate Sub UIrcvdDataHandler()

    Public Sub openCom()

        serPort.PortName = "COM1"
        serPort.Open()
        serPort.BaudRate = 38400
        serPort.ReceivedBytesThreshold = 10 ' set bigger than largest message expected, EOF will trigger DataReceived event

    End Sub

    Private Sub Receiver(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles serPort.DataReceived

        ' invoke handler in UI thread
        Form1.BeginInvoke(New UIrcvdDataHandler(AddressOf Form1.processRcvdData))
        'Form1.beginInvoke(New MethodInvoker(AddressOf Form1.processRcvdData))

        ' either of the invoke methods above produces the following error
        '"Invoke or BeginInvoke cannot be called on a control until the window handle has been created."

    End Sub
End Class
#End Region
Posted

Use the OnShown event instead of the Load event. The load event occurs before the UI elements are created and you are attempting to invoke on a control that exists, but hasn't been assigned a handle.
 
Share this answer
 
Comments
Member 10530942 17-Jan-14 12:44pm    
I added a .Shown event handler and moved the sp.openCom() there. It does get called but the invoke error still occurs. The original .Load code did a handle check(as shown in code) and confirmed its existence. Any other ideas?
Ron Beyer 17-Jan-14 13:45pm    
If this isn't working, in the receiver just check that Form1.IsHandleCreated is true before calling invoke.
I solved it with help from another programmer.
I added a shared property in the serPort_cls class. In Form1 I assigned me to it. I then changed Form1.beginInvoke() to propertyName.beginInvoke() and it works fine.
Thanks
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900