Windows collection events unreliable

J

J Kallay

I posted this on microsoft.public.visio.developer but haven't seen any
follow-ups, so I'll try again here.

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.
 
P

Paul Herber

I posted this on microsoft.public.visio.developer but haven't seen any
follow-ups, so I'll try again here.

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).

Are you taking into account the change in Windows collection index
number when any window gets closed and others are opened?
 
J

J Kallay

I don't see how that applies in this situation. The code that I included in
my post did not use any indexes, it added event handlers to a document's
windows collection's closing events. If a window is closed in that
document, the handler should be called.
 
M

Mark Nelson [MS]

Is m_ActiveWin a global variable that stays in scope for the entire Visio
application session?

--
Mark Nelson
Office Graphics - Visio
Microsoft Corporation

This posting is provided "AS IS" with no warranties, and confers no rights.

J Kallay said:
I don't see how that applies in this situation. The code that I included
in my post did not use any indexes, it added event handlers to a document's
windows collection's closing events. If a window is closed in that
document, the handler should be called.
 
J

J Kallay

m_activewin is a field of an object encapsulating a document and its
associated window (which are passed to the object's constructor). To make a
long story short, yes, the variable stays in scope until the document is
closed.

--
Jonathan Kallay
Visimation Inc.
www.visimation.com
www.shapesource.com
Mark Nelson said:
Is m_ActiveWin a global variable that stays in scope for the entire Visio
application session?

--
Mark Nelson
Office Graphics - Visio
Microsoft Corporation

This posting is provided "AS IS" with no warranties, and confers no
rights.
 
J

J Kallay

OK, I understand part of the problem now, and there's a mistake in my
original post which obscured it.
The line m_activewin.WindowClosing, etc. was actually supposed to be
m_activewin.Windows.WindowsClosing. And while m_activewin is kept in scope
so long as the document is open, I guess m_activewin.Windows is a temporary
object that is not.
 

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