which way to the event?

Started by perry59, October 26, 2016, 10:56:20 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

perry59

I have seen two methods (in vb.net) to implement event handling.
one is using the event sink methodology which seems convoluted and more difficult. The other method is using "AddHandler" which seems pretty straight forward. (example below)
is one method better than the other? why would I use one over the other?

here is some trimmed code from "treeview" in the SDK


Private documentClosedEvent As [Event]

        Private Sub addAdviseEvents()
            Dim applicationEventList As EventList
            applicationEventList = visioApplication.EventList
             ' Listen to DocumentClosed event.
            If (documentClosedEvent Is Nothing) Then
                documentClosedEvent = applicationEventList.AddAdvise( _
                    VisEventCodes.visEvtDel + VisEventCodes.visEvtDoc, _
                    Me, "", "")
            End If
        End Sub

        Private Sub handleDocumentClosed(ByVal closedDocument As Document)
            Dim taskDrawing As TaskDrawing
            taskDrawing = Me.getTaskDrawing(closedDocument)
            If (Not taskDrawing Is Nothing) Then
                taskDrawing.Dispose()
                documentDrawingHash.Remove(closedDocument)
            End If
            If (documentDrawingHash.Count = 0) Then
                unAdviseEvents()
                deleteCustomMenu()
            End If
        End Sub

        Protected Function ProcessVisioEvent(ByVal eventCode As Short, _
            Try
                If (visioApplication.IsUndoingOrRedoing = True) Then
                    Return False
                End If
                Select Case localEvent
                    Case CShort(VisEventCodes.visEvtDel _
                        + VisEventCodes.visEvtDoc)
                        ' A document has been closed.
                        documentSubject = CType(subject, Document)
                        handleDocumentClosed(documentSubject)
                End Select
            Catch err As ApplicationException
                LogException(err)
                DisplayException(visioApplication, err)
            Catch err As System.Runtime.InteropServices.COMException
                LogException(err)
                DisplayException(visioApplication, err)
            End Try
            Return False
        End Function

        Private Sub unAdviseEvents()
            If Not (documentClosedEvent Is Nothing) Then
                documentClosedEvent.Enabled = 0
                documentClosedEvent.Delete()
                Marshal.ReleaseComObject(documentClosedEvent)
                documentClosedEvent = Nothing
            End If
        End Sub


and here is some from my addin using Nikolay's template. This is a LOT easier to understand and seems to work just fine!


Public Sub New(addIn As ThisAddIn)
        ThisAddIn = addIn
        AddHandler ThisAddIn.Application.DocumentOpened, AddressOf OnDocumentOpened
    End Sub

    Public Sub Dispose() Implements IDisposable.Dispose
        RemoveHandler ThisAddIn.Application.DocumentOpened, AddressOf OnDocumentOpened
    End Sub

Private Sub OnDocumentOpened(doc As Microsoft.Office.Interop.Visio.Document)
'do your stuff here
end sub
what, me worry?

perry59

what, me worry?

wapperdude

Sorry Perry59, I have no experience with this. 

Wapperdude
Visio 2019 Pro

perry59

Quote from: wapperdude on October 28, 2016, 03:49:55 PM
Sorry Perry59, I have no experience with this. 

Wapperdude

Thanks!
there must be someone out there smarter than us :o
what, me worry?

wapperdude

Smarter???   :o  Nah!   ::)   More experienced... ;)

;D

Wapperdude
Visio 2019 Pro

Visisthebest

Still a great question hope someone will answer it!
Visio 2021 Professional

Visisthebest

Perry59 you could also ask this question in the Stackoverflow forum.
Visio 2021 Professional