Author Topic: InvalidOperationException was unhandled in AllenBradleyPCCC  (Read 1489 times)

wimsettj

  • Newbie
  • *
  • Posts: 16
    • View Profile
InvalidOperationException was unhandled in AllenBradleyPCCC
« on: October 27, 2014, 12:34:42 PM »
I am getting this error "InvalidOperationException was unhandled" when switching from Form6 to my Main Form with a Form change button. It does not happen every time. Sometimes I can change 5 times in a row and other times I can't change once without the error.

My Form6 is opened by clicking any selection button on Forms 1 through 4. This action also transfers the name of the selection to a BasicLabel on From6. I don't know if the error is happening because the original form is still open in the background when I leave Form6 or if something else is going on.
This is what I'm using for Forms1-4
Code: [Select]
Public Class Form1

    Private Sub Form_VisibleChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.VisibleChanged
        If components IsNot Nothing Then
            Dim drv As AdvancedHMIDrivers.IComComponent
            '*****************************
            '* Search for comm components
            '*****************************
            For i As Integer = 0 To components.Components.Count - 1
                If components.Components(i).GetType.GetInterface("AdvancedHMIDrivers.IComComponent") IsNot Nothing Then
                    drv = components.Components.Item(i)
                    '* Stop/Start polling based on form visibility
                    drv.DisableSubscriptions = Not Me.Visible
                End If
            Next
        End If
    End Sub

    Private DrinkName As String

    Private Sub LongIsland_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LongIsland.Click
        DrinkName = "Long Island"
        LoadForm6(DrinkName)
    End Sub

    Private Sub LongBeach_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LongBeach.Click
        DrinkName = "Long Beach"
        LoadForm6(DrinkName)
    End Sub
    Private Sub LoadForm6(ByVal DrinkName As String)
        Dim oForm As New Form6
        oForm.SetDrinkName = DrinkName
        oForm.Selection.Text = DrinkName
        oForm.ShowDialog()
        Me.Close()
    End Sub

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

    End Sub
End Class
I tried adding in the Me.Close() because the Form was staying open in the background but it doesn't seem to work.

My Form6 code
Code: [Select]
Public Class Form6

    Private Sub Form_VisibleChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.VisibleChanged
        If components IsNot Nothing Then
            Dim drv As AdvancedHMIDrivers.IComComponent
            '*****************************
            '* Search for comm components
            '*****************************
            For i As Integer = 0 To components.Components.Count - 1
                If components.Components(i).GetType.GetInterface("AdvancedHMIDrivers.IComComponent") IsNot Nothing Then
                    drv = components.Components.Item(i)
                    '* Stop/Start polling based on form visibility
                    drv.DisableSubscriptions = Not Me.Visible
                End If
            Next
        End If
    End Sub

    Private DrinkName As String = ""

    Public WriteOnly Property SetDrinkName As String
        Set(ByVal value As String)
            DrinkName = value
        End Set
    End Property

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

    End Sub
End Class

Any suggestions? I'm down to the wire on getting this project done on time.

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5262
    • View Profile
    • AdvancedHMI
Re: InvalidOperationException was unhandled in AllenBradleyPCCC
« Reply #1 on: October 27, 2014, 02:09:09 PM »
This is kind of a race condition where the driver tries to send data to the control before the control is fully initialized. The easiest fix is to just ignore the error by modifying AllenBradleyPCCC.vb like this:

Code: [Select]
Try
        SynchronizingObject.BeginInvoke(SubscriptionList(i), z)
Catch ex as InvalidOperationException
End try

wimsettj

  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: InvalidOperationException was unhandled in AllenBradleyPCCC
« Reply #2 on: October 27, 2014, 05:26:47 PM »
Archie,

Thank you for the quick reply. I will give it a try tonight when I get off work. Does it matter where I insert this in the code?
« Last Edit: October 27, 2014, 05:33:09 PM by wimsettj »

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5262
    • View Profile
    • AdvancedHMI
Re: InvalidOperationException was unhandled in AllenBradleyPCCC
« Reply #3 on: October 27, 2014, 07:28:18 PM »
Does it matter where I insert this in the code?
Around the line of code in AllenBradleyPCCC.vb that throws the exception

wimsettj

  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: InvalidOperationException was unhandled in AllenBradleyPCCC
« Reply #4 on: October 28, 2014, 01:46:14 AM »
 Archie,

:) THANK YOU!!! It worked. The automated bar is up and running flawlessly!

Your AdvancedHMI is amazing.  Thank you for all your hard work on this awesome program and for your help in trouble shooting my issues.