6 Replies Latest reply on Aug 2, 2016 7:50 AM by Jesse Seger

    Please Help- Intermitent AddHandler Bug?

    Edwin Thompson

      This project works nicely which allows various information to be retrieved from a SW document.  The goal is to automatically re-establish events when the document is changed.  The original approach would sometime generate two identical events which would run simultaneously and crated problems.  This was a significant issue so the approach below is to always Remove the handler before attempting to create a new one with the new document.  Therefore, even if AttachSWEvents is called more than once, there would only ever be one event started for the current document.  This works very nicely, except after a randomly a document will not "register" properly. 

       

      Surprisingly, if the current document is changed to a different document and then refocused on, many times the Event can be re-established.

       

      In addition to the typical overhead for events (swApp, swModel, etc.) there is the AttachSWEvents sub:

       

      Sub AttachSWEvents()

                  Try  'To prevent multiple events from being registered, always remove the current event

                      RemoveHandler msrcPartDoc.UserSelectionPostNotify, AddressOf msrcPartDoc_NewSelectionNotify

                      RemoveHandler msrcPartDoc.destroynotify2, AddressOf msrcPartDoc_DestroyNotify

                      Debug.Print("*****" + swModel.GetPathName + " Part Removed")

                  Catch ex As Exception

                      Debug.Print("***failed to Remove Part Event Handler")

                  End Try

                  Try  'Add the new event for the loaded document

                     AddHandler msrcPartDoc.UserSelectionPostNotify, AddressOf msrcPartDoc_NewSelectionNotify

                      Debug.Print("*****" + swModel.GetPathName + " Part Added")

                   Catch ex As Exception

                      Debug.Print("***** failed to Register Part Handler")

                  End Try

                  Try  'Add the new destroy event for the loaded document

                      AddHandler msrcPartDoc.destroynotify2, AddressOf msrcPartDoc_DestroyNotify

                  Catch ex As Exception

                      Debug.Print("**** failed to Register Destroy Part Handler")

                  End Try

      End Sub

       

      Private Function swApp_ActiveDocChangeNotify() As Long

               msrcPartDoc = Nothing 'The main program detects when = nothing, re-established the current document and activates the AttachSWEvents() Sub

               swModel = Nothing

      End Function

       

      This all works fine. However, sometimes after as few as three document change and as many as 16 document changes, the new Events will create and error.

       

      Part of the VS Result window is shown below:

      The Line shown in Red Above is the code that fails (once in awhile).  Any idea why this happens so randomly?  Any ideas how to correct this?

        • Re: Please Help- Intermitent AddHandler Bug?
          Jesse Seger

          I noticed in your output that it is having trouble subscribing to a handler that has been previously registered (Rib Top.SLDPRT) even though you remove it first.  Is this a correct assumption?  That the exception happens when the event has already been subscribed to?

            • Re: Please Help- Intermitent AddHandler Bug?
              Edwin Thompson

              I really appreciate your help.  If I am following your thinking correctly I made a few changes with better debug descriptions.  The results below should be much clearer.  Also with your suggestion I tried a number of potential changes but none of them appeared to work.  Any suggestions?

               

              It appears that the event handlers are "stored" into the background of Solidworks.  Is this correct?  The RemoveHandler appears to remove it but not necessarily "destroy" it, as there is some evidence that it is still hanging around in the background.  Do you know that this is correct?

                • Re: Please Help- Intermitent AddHandler Bug?
                  Jesse Seger

                  Well... Lemme ask you this.  Is this an addin or a stand alone application?

                    • Re: Please Help- Intermitent AddHandler Bug?
                      Edwin Thompson

                      Stand alone.  In fact, the whole exercise of the addhandlers is for the program to sit quietly on the desk top where some of the functions work but others are turned off until SW is started and a document is opened.  I actually have most of this working except for  this little intermittent bug.

                       

                      If I can get this working properly, there are some possibilities of working with draftsight and potentially other related programs.

                       

                      One question out of this is that it appears that the addhandler actually resides in Solidworks.  I need to verify this because there is some evidence that as I have made modifications to the code some of the results have been unexpected because some of the addhandlers may have remained running in the background of SW.  Do you know if this true or not?

                        • Re: Please Help- Intermitent AddHandler Bug?
                          Jesse Seger

                          I've seen this behavior before.  It's usually the result of mishandling objects.  For example, getting a reference to ActiveDoc.  This returns a new reference, but the same underlying com object.  So RemoveHandler doesn't actually remove event handlers on the reference you created.  The trick is you have to keep your reference around yourself.  Maybe in a HashSet.  Then when you need it, locate it in the HashSet and remove/add your handlers.

                        • Re: Please Help- Intermitent AddHandler Bug?
                          Edwin Thompson

                          One never knows for sure about intermittent errors but it appears that this problem has been solved.  The problem seems to have been that multiple AddHandler events can cause issues.  While I was being careful about the individual document events, when there was more than one of the primary event handler for detecting changes to the current document would fire as many times as the handler was loaded.  Probably creating some sort of issue.  When only a single AddHandler was allowed the program started to behave as expected. 

                           

                          Any thoughts?

                           

                          Thank you.