4 Replies Latest reply on Jul 4, 2016 6:24 AM by John Alexander

    Strange Error

    Edwin Thompson

      This line of code:  "Me.Text2.Text = "Select Second End Point" "  generated this error:

      A first chance exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll

      When the form loads, similar text is placed into the box.  Now later on, I'm surprised that just placing text into a text box creates a problem?

       

      Any ideas?

      I appreciate any help possible on this one.

        • Re: Strange Error
          Deepak Gupta

          This could be related to other things also. Any chance if you can upload the complete program/project to check/debug.

            • Re: Strange Error
              Edwin Thompson

              After a fair amount of work, the problem is related to  the selection manager with Solidworks. 

              The VB.net program is "tied" to Solidworks.  By using the selection manager, when an entity is from the Solidworks window, the 3D point is selected and per the three lines of code below, VB.net jumps to the PartMeasure function where a fair amount of activity occurs that ultimately calculates the distance between a second selection (prompted by the Text2 text)..

                      swSelMgr = swModel.SelectionManager  'very important for selection to work

                      swPartDoc = swModel

                       AddHandler swPartDoc.UserSelectionPostNotify, AddressOf PartMeasure

              When in the PartMeasure Function Text2.text will not update and creates an error. 

              A button was added to the program that jumps to PartMeasure (independently of the SelectionManager) and Text2.text updates without an error.

              I'm sure that this is the problem but don't have a clue about how to work around this.

                • Re: Strange Error
                  John Alexander

                          swSelMgr = swModel.SelectionManager  'very important for selection to work

                          swPartDoc = swModel

                           AddHandler swPartDoc.UserSelectionPostNotify, AddressOf PartMeasure

                  Could you post the whole module/more context or explain why you need to assign swModel.SelectionManager to swSelMgr? I don't see it is being used.

                   

                   

                  Regarding your first post, I'm guessing that something about the state of the form is preventing it from assigning to the Text2.text property.

                   

                  From MSDN: InvalidOperationException Class (System)

                  Remarks

                  https://msdn.microsoft.com/en-us/library/system.invalidoperationexception(v=vs.110).aspx#Anchor_6

                  InvalidOperationException is used in cases when the failure to invoke a method is caused by reasons other than invalid arguments. Typically, it is thrown when the state of an object cannot support the method call. For example, an InvalidOperationException exception is thrown by methods such as:

                   

                  I'm not sure how the event handlers behave - do they launch the callback functions on a separate thread? That might be worth investigating.

                  • Re: Strange Error
                    Artem Taturevych

                    Hi Edwin,

                     

                    This is most likely synchronization issue.

                     

                    You need to use Invoke method:

                     

                    Dim del As New Action(Of System.Windows.Forms.Control)(AddressOf SetText)

                    Me.Text2.Invoke(del, Me.Text2)

                     

                    and add the following function

                     

                    Sub SetText(ByVal ctrl As System.Windows.Forms.Control)

                        ctrl.Text = "Select Second End Point"

                    End Sub

                     

                    If you are using Framework 4.0 or newer than you can simply use

                     

                            Me.Text2.Invoke(Sub()

                                                Me.Text2.Text = "Select Second End Point"

                                            End Sub)

                    Thanks,
                    Artem