Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. problem with System.InvalidOperationException - enabling buttons from within event sub

problem with System.InvalidOperationException - enabling buttons from within event sub

Scheduled Pinned Locked Moved Visual Basic
helpdatabasewpf
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    abiemann
    wrote on last edited by
    #1

    I don't know why I'm getting this InvalidOperationException - so I'm really eager to find an explanation and learn from this bug. the code:

    Imports myprog.Control_DataCapsule 'this is a control containing several text boxes and buttons
    Class Window1
    Private MSVMobject As ClassMSVM = Nothing

    Private Sub Window1\_Loaded()
    
        Try
            MSVMobject = New ClassMSVM
            AddHandler MSVMobject.VMexited, AddressOf Me.MSVM\_VMexited
        Catch
            MsgBox("MSVM is not available on this machine.")
        End Try
    
    End Sub
    
    Friend Sub MSVM\_VMexited()
        .
        .
        .
        Dim OdataCapsule As Control\_DataCapsule 'causes an invalidOperationException
        .
        .
        .
    End Sub
    
    Private Sub StackPanel\_ButtonClick(ByVal sender As Object, ByVal e As RoutedEventArgs)
    
        MSVMobject.StartVM()
    
    End Sub
    

    End Class

    Public Class ClassMSVM

    Public Event VMexited()
    
    Public Sub StartVM()
            .
            .
            .
            Dim myProcess As Process = New Process()
            myProcess.StartInfo.FileName = strStartupPath + "vmconnect.exe"
            myProcess.StartInfo.Arguments = "localhost " + VMname
            myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
            myProcess.StartInfo.CreateNoWindow = True
            myProcess.EnableRaisingEvents = True
            AddHandler myProcess.Exited, AddressOf Me.VMconnectExited
            myProcess.Start()
    End Sub
    
    Friend Sub VMconnectExited(ByVal sender As Object, ByVal e As System.EventArgs)
    
        Dim myProcess As Process = DirectCast(sender, Process)
        myProcess.Close()
    
        'trigger event that will be captured in Window1.xaml.vb
        RaiseEvent VMexited()
    
    End Sub
    

    End Class

    'in a separate vb file...
    Partial Public Class Control_DataCapsule

    'member variables
    Public m\_iIndexInList As Integer
    
    Public Sub New()
        ' This call is required by the Windows Form Designer.
        InitializeComponent()
    
        m\_iIndexInList = 0
    End Sub
    '\*\*\* SET \*\*\*
    Public Sub SetTitle(ByVal strTitle As String)
        Me.TextBoxTitle.Text = strTitle
    End Sub
    .
    .
    .
    

    End Class

    What happens is that after I click a button in my stackpanel, MSVM is launched. This works great. Once I close MSVM, the MSVM_VMexited() sub starts to execute but as soon as it gets to "Dim OdataCapsule As Control_DataCapsule" an I

    A L 3 Replies Last reply
    0
    • A abiemann

      I don't know why I'm getting this InvalidOperationException - so I'm really eager to find an explanation and learn from this bug. the code:

      Imports myprog.Control_DataCapsule 'this is a control containing several text boxes and buttons
      Class Window1
      Private MSVMobject As ClassMSVM = Nothing

      Private Sub Window1\_Loaded()
      
          Try
              MSVMobject = New ClassMSVM
              AddHandler MSVMobject.VMexited, AddressOf Me.MSVM\_VMexited
          Catch
              MsgBox("MSVM is not available on this machine.")
          End Try
      
      End Sub
      
      Friend Sub MSVM\_VMexited()
          .
          .
          .
          Dim OdataCapsule As Control\_DataCapsule 'causes an invalidOperationException
          .
          .
          .
      End Sub
      
      Private Sub StackPanel\_ButtonClick(ByVal sender As Object, ByVal e As RoutedEventArgs)
      
          MSVMobject.StartVM()
      
      End Sub
      

      End Class

      Public Class ClassMSVM

      Public Event VMexited()
      
      Public Sub StartVM()
              .
              .
              .
              Dim myProcess As Process = New Process()
              myProcess.StartInfo.FileName = strStartupPath + "vmconnect.exe"
              myProcess.StartInfo.Arguments = "localhost " + VMname
              myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
              myProcess.StartInfo.CreateNoWindow = True
              myProcess.EnableRaisingEvents = True
              AddHandler myProcess.Exited, AddressOf Me.VMconnectExited
              myProcess.Start()
      End Sub
      
      Friend Sub VMconnectExited(ByVal sender As Object, ByVal e As System.EventArgs)
      
          Dim myProcess As Process = DirectCast(sender, Process)
          myProcess.Close()
      
          'trigger event that will be captured in Window1.xaml.vb
          RaiseEvent VMexited()
      
      End Sub
      

      End Class

      'in a separate vb file...
      Partial Public Class Control_DataCapsule

      'member variables
      Public m\_iIndexInList As Integer
      
      Public Sub New()
          ' This call is required by the Windows Form Designer.
          InitializeComponent()
      
          m\_iIndexInList = 0
      End Sub
      '\*\*\* SET \*\*\*
      Public Sub SetTitle(ByVal strTitle As String)
          Me.TextBoxTitle.Text = strTitle
      End Sub
      .
      .
      .
      

      End Class

      What happens is that after I click a button in my stackpanel, MSVM is launched. This works great. Once I close MSVM, the MSVM_VMexited() sub starts to execute but as soon as it gets to "Dim OdataCapsule As Control_DataCapsule" an I

      A Offline
      A Offline
      abiemann
      wrote on last edited by
      #2

      what I've done now is declare OdataCapsule as a member variable of Window1 (temporarily called it "myOdataCapsule" for this experiment), and the program gets farther... however, now the following fails:

      myOdataCapsule = DirectCast(Me.StackPanel1.Children.Item(iSPindex), Control_DataCapsule)

      myOdataCapsule always equals Nothing ! This is a problem since I used the DirectCast to gain access to methods in Control_DataCapsule (many of these are stored in the stackpanel) to enable and disable buttons on the controls. Overall goal Somehow I need to step through the Control_DataCapsule's in the stackpanel once Window1 receives the VMexited event. My implementation above is the only way I thought I could :((

      1 Reply Last reply
      0
      • A abiemann

        I don't know why I'm getting this InvalidOperationException - so I'm really eager to find an explanation and learn from this bug. the code:

        Imports myprog.Control_DataCapsule 'this is a control containing several text boxes and buttons
        Class Window1
        Private MSVMobject As ClassMSVM = Nothing

        Private Sub Window1\_Loaded()
        
            Try
                MSVMobject = New ClassMSVM
                AddHandler MSVMobject.VMexited, AddressOf Me.MSVM\_VMexited
            Catch
                MsgBox("MSVM is not available on this machine.")
            End Try
        
        End Sub
        
        Friend Sub MSVM\_VMexited()
            .
            .
            .
            Dim OdataCapsule As Control\_DataCapsule 'causes an invalidOperationException
            .
            .
            .
        End Sub
        
        Private Sub StackPanel\_ButtonClick(ByVal sender As Object, ByVal e As RoutedEventArgs)
        
            MSVMobject.StartVM()
        
        End Sub
        

        End Class

        Public Class ClassMSVM

        Public Event VMexited()
        
        Public Sub StartVM()
                .
                .
                .
                Dim myProcess As Process = New Process()
                myProcess.StartInfo.FileName = strStartupPath + "vmconnect.exe"
                myProcess.StartInfo.Arguments = "localhost " + VMname
                myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
                myProcess.StartInfo.CreateNoWindow = True
                myProcess.EnableRaisingEvents = True
                AddHandler myProcess.Exited, AddressOf Me.VMconnectExited
                myProcess.Start()
        End Sub
        
        Friend Sub VMconnectExited(ByVal sender As Object, ByVal e As System.EventArgs)
        
            Dim myProcess As Process = DirectCast(sender, Process)
            myProcess.Close()
        
            'trigger event that will be captured in Window1.xaml.vb
            RaiseEvent VMexited()
        
        End Sub
        

        End Class

        'in a separate vb file...
        Partial Public Class Control_DataCapsule

        'member variables
        Public m\_iIndexInList As Integer
        
        Public Sub New()
            ' This call is required by the Windows Form Designer.
            InitializeComponent()
        
            m\_iIndexInList = 0
        End Sub
        '\*\*\* SET \*\*\*
        Public Sub SetTitle(ByVal strTitle As String)
            Me.TextBoxTitle.Text = strTitle
        End Sub
        .
        .
        .
        

        End Class

        What happens is that after I click a button in my stackpanel, MSVM is launched. This works great. Once I close MSVM, the MSVM_VMexited() sub starts to execute but as soon as it gets to "Dim OdataCapsule As Control_DataCapsule" an I

        A Offline
        A Offline
        abiemann
        wrote on last edited by
        #3

        I then decided an alternative might be just to disable and enable the stackpanel on the Window1

        Private Sub StackPanel\_ButtonClick(ByVal sender As Object, ByVal e As RoutedEventArgs)
            Me.StackPanel1.IsEnabled = False 'instead of individually disabling buttons in each control on the stackpanel
            MSVMobject.StartVM()
        End Sub
        
        Friend Sub MSVM\_VMexited()
            Me.StackPanel1.IsEnabled = True
        End Sub
        

        However, "Me.StackPanel1.IsEnabled = True" still caused the exception X| :(( :confused:

        1 Reply Last reply
        0
        • A abiemann

          I don't know why I'm getting this InvalidOperationException - so I'm really eager to find an explanation and learn from this bug. the code:

          Imports myprog.Control_DataCapsule 'this is a control containing several text boxes and buttons
          Class Window1
          Private MSVMobject As ClassMSVM = Nothing

          Private Sub Window1\_Loaded()
          
              Try
                  MSVMobject = New ClassMSVM
                  AddHandler MSVMobject.VMexited, AddressOf Me.MSVM\_VMexited
              Catch
                  MsgBox("MSVM is not available on this machine.")
              End Try
          
          End Sub
          
          Friend Sub MSVM\_VMexited()
              .
              .
              .
              Dim OdataCapsule As Control\_DataCapsule 'causes an invalidOperationException
              .
              .
              .
          End Sub
          
          Private Sub StackPanel\_ButtonClick(ByVal sender As Object, ByVal e As RoutedEventArgs)
          
              MSVMobject.StartVM()
          
          End Sub
          

          End Class

          Public Class ClassMSVM

          Public Event VMexited()
          
          Public Sub StartVM()
                  .
                  .
                  .
                  Dim myProcess As Process = New Process()
                  myProcess.StartInfo.FileName = strStartupPath + "vmconnect.exe"
                  myProcess.StartInfo.Arguments = "localhost " + VMname
                  myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
                  myProcess.StartInfo.CreateNoWindow = True
                  myProcess.EnableRaisingEvents = True
                  AddHandler myProcess.Exited, AddressOf Me.VMconnectExited
                  myProcess.Start()
          End Sub
          
          Friend Sub VMconnectExited(ByVal sender As Object, ByVal e As System.EventArgs)
          
              Dim myProcess As Process = DirectCast(sender, Process)
              myProcess.Close()
          
              'trigger event that will be captured in Window1.xaml.vb
              RaiseEvent VMexited()
          
          End Sub
          

          End Class

          'in a separate vb file...
          Partial Public Class Control_DataCapsule

          'member variables
          Public m\_iIndexInList As Integer
          
          Public Sub New()
              ' This call is required by the Windows Form Designer.
              InitializeComponent()
          
              m\_iIndexInList = 0
          End Sub
          '\*\*\* SET \*\*\*
          Public Sub SetTitle(ByVal strTitle As String)
              Me.TextBoxTitle.Text = strTitle
          End Sub
          .
          .
          .
          

          End Class

          What happens is that after I click a button in my stackpanel, MSVM is launched. This works great. Once I close MSVM, the MSVM_VMexited() sub starts to execute but as soon as it gets to "Dim OdataCapsule As Control_DataCapsule" an I

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          Hi, I'm not familiar with RaiseEvent as I work mainly in C#, however it seems to me you are having a typical invalid cross-thread situation: the VM signals its exit on some thread (probably a ThreadPool thread) because it is fully asynchronous and MS didn't want to force it onto the main thread. So IMO you need the same Control.InvokeRequired/Control.Invoke stuff you would need when trying to access some Control from a secondary thread, see here[^]. BTW: I don't really believe Dim OdataCapsule As Control_DataCapsule was ever throwing an exception as this statement is purely declarative, there is nothing executing there. :)

          Luc Pattyn


          Have a look at my entry for the lean-and-mean competition; please provide comments, feedback, discussion, and don’t forget to vote for it! Thank you.


          Local announcement (Antwerp region): Lange Wapper? Neen!


          A 1 Reply Last reply
          0
          • L Luc Pattyn

            Hi, I'm not familiar with RaiseEvent as I work mainly in C#, however it seems to me you are having a typical invalid cross-thread situation: the VM signals its exit on some thread (probably a ThreadPool thread) because it is fully asynchronous and MS didn't want to force it onto the main thread. So IMO you need the same Control.InvokeRequired/Control.Invoke stuff you would need when trying to access some Control from a secondary thread, see here[^]. BTW: I don't really believe Dim OdataCapsule As Control_DataCapsule was ever throwing an exception as this statement is purely declarative, there is nothing executing there. :)

            Luc Pattyn


            Have a look at my entry for the lean-and-mean competition; please provide comments, feedback, discussion, and don’t forget to vote for it! Thank you.


            Local announcement (Antwerp region): Lange Wapper? Neen!


            A Offline
            A Offline
            abiemann
            wrote on last edited by
            #5

            Hi Luc, I've tried it, but WPF seems to be different because I get the error: InvokeRequired' is not a member of 'System.Windows.Controls.StackPanel so I'll try something like this: http://blog.somecreativity.com/2008/01/10/wpf-equivalent-of-invokerequired/[^]

            L 1 Reply Last reply
            0
            • A abiemann

              Hi Luc, I've tried it, but WPF seems to be different because I get the error: InvokeRequired' is not a member of 'System.Windows.Controls.StackPanel so I'll try something like this: http://blog.somecreativity.com/2008/01/10/wpf-equivalent-of-invokerequired/[^]

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              As I am unexperienced in WPF, I won't be able to help you. Sorry. :)

              Luc Pattyn


              Have a look at my entry for the lean-and-mean competition; please provide comments, feedback, discussion, and don’t forget to vote for it! Thank you.


              Local announcement (Antwerp region): Lange Wapper? Neen!


              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups