Unreliable Windows object events

J

J Kallay

I am seeing some very serious and strange problems with the raising of
events from a Windows collection. Specifically, I'm looking at
BeforeWindowClosed and QueryCancelWindowClose events (with the intent of
detecting when an add-on anchor window is being closed). In VBA, the "Got
here" line is printed reliably whenever windows such as the Pan and Zoom or
Shape Data window are closed.

Dim WithEvents vsoWindows As Windows
Public Sub Test()
Set vsoWindows = ActiveWindow.Windows

End Sub

Private Sub vsoWindows_BeforeWindowClosed(ByVal Window As IVWindow)
Debug.Print "Got here"
End Sub

However, in a C# add-in:

Using C# delegates, i.e.
m_activewin.BeforeWindowClosed+=new
Microsoft.Office.Interop.Visio.EWindow_BeforeWindowClosedEventHandler(Window_BeforeWindowClosed);

the delegate method will not get called on the first document I open and set
up with events. When I close that document and open a second one, the
method will get called once when I close one of the windows, but will never
get called again.

Using the AddAdvise method, i.e.
object eventHandler = new EventSink();
Visio.EventList eventsWindow =m_activewin.Windows.EventList;
eventsWindow.AddAdvise((short)Visio.VisEventCodes.visEvtDel+(short)Visio.VisEventCodes.visEvtWindow,
eventHandler, "", "");

eventHandler.VisEventProc will not get called at all.

Using the delegate method AND VisEventProc together is where it gets
interesting:
1) The delegate method will not get called.
2) eventHandler.VisEventProc will get called once every time a window is
closed on the first, third, etc. document that I open and close.
3) eventHandler.VisEventProc will get called twice every time a window is
closed on the second, fourth, etc. document. Note that I do not ever remove
the delegate event handler when a document is closed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top