Managed menu handlers quit responding soon after launch

  • Thread starter Paul F. Williams
  • Start date
P

Paul F. Williams

I created a C# Visio COM add-in using the interop DLL. The add-in inserts a
new top-level menu hierarchy into Visio's bar using CommandBar controls.
The menus appear and are functional... for a while.

Soon, the menu items stop invoking my event handlers. All of them stop
working at once, which leads me to believe that something may be getting
garbage collected. I tried making my handlers member variables-- to no
avail. There is no error message, not even when I turn on breaking on all
exceptions. They just doesn't work. I can let Visio sit a while after
startup, or I can open a document and work. It always happens.

Has anyone else seen this problem? What's happening to my event handlers?

Here is some sample code I use to create the menus:

CommandBarPopup dialogFlowMenu = (CommandBarPopup)menuBar.Controls.Add
(Microsoft.Office.Core.MsoControlType.msoControlPopup, Missing.Value,
Missing.Value, beforeControl.Index, Missing.Value);
dialogFlowMenu.Caption = "&Dialog";
dialogFlowMenu.Tag = DIALOG_MENU_TAG;
...
CommandBarButton exportMenu =
(CommandBarButton)dialogFlowMenu.Controls.Add
(MsoControlType.msoControlButton, Missing.Value, Missing.Value,
Missing.Value, Missing.Value);
exportMenu.Caption = "&Export";
exportMenu.Tag = DIALOG_MENU_EXPORT_TAG;
...
_CommandBarButtonEvents_ClickEventHandler exportHandler = new
_CommandBarButtonEvents_ClickEventHandler(exportMenu_Click);
exportMenu.Click += exportHandler;
 
B

Blair [MS]

What is the scope of variable used to hold the menu?

If this variable goes out of scope, then a GC is triggered and the behavior
you reported will be seen.

I found the solution was to move the variable to the class level.

-blair [ms]
 
P

Paul F. Williams

Gee, that might be a good thing to look at. [slaps forehead]

My menus are now working correctly, but the Document- and Application-level
event handlers I have still disappear. In a normal C# app, I might say

SomeObject.SomeEvent += new EventHandler (SomeObject_SomeEvent);

I assume that defining the event handlers as member variables will help
avoid garbage collection:

private EventHandler someEventHandler;
...
someEventHandler = new EventHandler (SomeObject_SomeEvent);
SomeObject.SomeEvent += someEventHandler;

Blair said:
What is the scope of variable used to hold the menu?

If this variable goes out of scope, then a GC is triggered and the behavior
you reported will be seen.

I found the solution was to move the variable to the class level.

-blair [ms]

Paul F. Williams said:
I created a C# Visio COM add-in using the interop DLL. The add-in
inserts
a
new top-level menu hierarchy into Visio's bar using CommandBar controls.
The menus appear and are functional... for a while.

Soon, the menu items stop invoking my event handlers. All of them stop
working at once, which leads me to believe that something may be getting
garbage collected. I tried making my handlers member variables-- to no
avail. There is no error message, not even when I turn on breaking on all
exceptions. They just doesn't work. I can let Visio sit a while after
startup, or I can open a document and work. It always happens.

Has anyone else seen this problem? What's happening to my event handlers?

Here is some sample code I use to create the menus:

CommandBarPopup dialogFlowMenu = (CommandBarPopup)menuBar.Controls.Add
(Microsoft.Office.Core.MsoControlType.msoControlPopup, Missing.Value,
Missing.Value, beforeControl.Index, Missing.Value);
dialogFlowMenu.Caption = "&Dialog";
dialogFlowMenu.Tag = DIALOG_MENU_TAG;
...
CommandBarButton exportMenu =
(CommandBarButton)dialogFlowMenu.Controls.Add
(MsoControlType.msoControlButton, Missing.Value, Missing.Value,
Missing.Value, Missing.Value);
exportMenu.Caption = "&Export";
exportMenu.Tag = DIALOG_MENU_EXPORT_TAG;
...
_CommandBarButtonEvents_ClickEventHandler exportHandler = new
_CommandBarButtonEvents_ClickEventHandler(exportMenu_Click);
exportMenu.Click += exportHandler;
 

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