Click here to Skip to main content
Click here to Skip to main content

Crash while closing application that uses Windows Ribbon Framework

, 22 Apr 2010
Rate this:
Please Sign up or sign in to vote.
Since that’s the third time I’ve been asked about it, and in fact came across the problem myself, I thought I should blog my reply to help future users.Problem DescriptionYou use the Windows Ribbon Framework, either directly (in C++) or in managed code using my Windows Ribbon for WinForms library. Y

Since that’s the third time I’ve been asked about it, and in fact came across the problem myself, I thought I should blog my reply to help future users.

Problem Description

You use the Windows Ribbon Framework, either directly (in C++) or in managed code using my Windows Ribbon for WinForms library. You add a close button to the ribbon which closes the application. The application crash on close.

Don’t cut the branch you sit on

The problem is that you try to call ribbon.DestroyFramework, which ultimately calls IUIFramework.Destroy from a ribbon command handler. So while handling the ribbon event you try to kill the ribbon. It is only fair that the ribbon control fights back..

Solution

Either invoke the Close() method asynchronously:

void _exitButton_OnExecute(
    PropertyKeyRef key, 
    PropVariantRef currentValue, 
    IUISimplePropertySet commandExecutionProperties)
{
    // Close form asynchronously since we are in a ribbon event 
    // handler, so the ribbon is still in use, and calling Close 
    // will eventually call _ribbon.DestroyFramework(), which is 
    // a big no-no, if you still use the ribbon.
    this.BeginInvoke(new MethodInvoker(this.Close));
}

or don’t call DestroyFramework when closing the application (and trust windows for releasing the resources).

[By the way, the C++ solution is simply to call PostMessage(WM_CLOSE) instead of SendMessage(WM_CLOSE)]

I’ve updated sample 04-TabGroupHelp on the project site so that it has a real exit button on the ribbon that closes the form.

That’s it for now, Arik Poznanski.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)

About the Author

Arik Poznanski
Software Developer (Senior) Verint
Israel Israel
Arik Poznanski is a senior software developer at Verint. He completed two B.Sc. degrees in Mathematics & Computer Science, summa cum laude, from the Technion in Israel.
 
Arik has extensive knowledge and experience in many Microsoft technologies, including .NET with C#, WPF, Silverlight, WinForms, Interop, COM/ATL programming, C++ Win32 programming and reverse engineering (assembly, IL).
Follow on   Twitter   Google+

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web03 | 2.8.140709.1 | Last Updated 23 Apr 2010
Article Copyright 2010 by Arik Poznanski
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid