Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
799 views
in Technique[技术] by (71.8m points)

.net - CommandBars.OnUpdate stops being fired

In my PowerPoint 2010 Addin I subscribed to the CommandBars.OnUpdate event to register movement of shapes and similar events:

 ppt = Globals.ThisAddIn.Application;
 ppt.CommandBars.OnUpdate += CommandBars_OnUpdate;

This works great for some time. However, eventually the event stops being fired. Well, at least the registered event handler is not being called. I couldn't figure out, what causes this behaviour. It seems a bit indeterministic. No exception is thrown that would appear in the Debug output. It seems, this happens after some time and is not caused by a user action.

I assumed, this would be due to a change of the CommandBars object. So I added a timer, which checked for those changes. But both == and .Equals() comparisons to the last object result in a recognized change every tick, which is very unlikely.

I also tried to refresh the event handler periodically (every 1 minute), but this doesn't work either:

ppt.CommandBars.OnUpdate -= CommandBars_OnUpdate;
ppt.CommandBars.OnUpdate += CommandBars_OnUpdate;

Does the event really stop after some time? Is there any other method to detect object movement, resize, deletion etc.?


Update

In the meanwhile I restructured the addin. I am now able to reproduce the problem. Here is how:

In the addin's ribbon I have a button, which calls the method CreateRightEyeCopy() on the ViewModel. In this method another method GetNextPairId() of the ViewModel is called. And this call seems problematic. I changed the GetNextPairId() to immediately return 0 to ensure that the method is the problem.

Here is the resulting stack trace in the return 0 line:

ViewModel.GetNextPairId()
[External Code]
ViewModel.CreateRightEyeCopy()
Button's event handler

I wonder, why there is external code in between my two functions. Can this code cause the OnUpdate event to stop?

If someone's interested, here is the code of the two functions:

CreateRightEyeCopy():

try
{
    var sel = ppt.ActiveWindow.Selection;
    if (sel.Type == PpSelectionType.ppSelectionShapes)
    {
        foreach (Shape s in sel.ShapeRange)
        {
            var pair = FindStereoPair(s);
            //Only add a new pair, if shape is not in a pair already
            if (pair == null)
            {
                // ** return; **
                int id = GetNextPairId(s.Parent);
                return; //for debugging purposes
            }
        }
    }
}
catch (Exception x)
{
    Debug.WriteLine("Exception occured during creation of stereo pair: " + x.Message);
}

GetNextPairId():

return 0;

If I insert a return statement before the call to GetNextPairId(), then OnUpdate continues.

I also tried to invoke CreateRightEyeCopy() asynchronously, but that doesn't change anything.

Are there any further thoughts on this issue?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Not a real answer but MSDN says: "It is strongly recommended that this event be used primarily for checking that a custom command bar has been added or removed by a COM AddIn."

You should probably choose a different approach to track other events.

As an afterthought, are you sure you are capturing all exceptions (UnhandledException, ThreadException)?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...