problem with System.InvalidOperationException - enabling buttons from within event sub
-
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 = NothingPrivate 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
-
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 = NothingPrivate 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
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 :((
-
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 = NothingPrivate 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
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:
-
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 = NothingPrivate 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
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!
-
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!
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/[^]
-
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/[^]
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!