Search code examples
winformsinstallationwixwindows-installercustom-action

custom windows form on uninstall


I want to show my custom windows form on uninstall. I am going to use a C# custom action for this. How do I make the custom action wait till the user clicks OK or Cancel? I want my custom action to complete only when the form is closed. So do the following in my custom action:

    CollectUninstallData f = new CollectUninstallData();
            f.Show();
            return f.FormResult;

But the form blinks for a moment and the uninstall continues without waiting for the form to close. And this is logical since the GUI is in another thread. But how do I make it wait for the form to close?

I am aware that showing custom windows forms in install packages isn't cool, so if anyone can offer a more elegant solution, then I will thankfully accept it.


Solution

  • You have to use ShowDialog() method instead of Show(). The latter makes the form visible and returns control that's why your Custom Action stops its execution. The former shows the form as a modal dialog box and does not return until the user closes the form in any way.

    CollectUninstallData f = new CollectUninstallData();
    DialogResult r = f.ShowDialog();
    f.Dispose(); 
    return r;
    

    If you want to know whether user clicked OK or Cancel, use this statement return r == DialogResult.OK ? 0 : 1. Return code of zero usually indicates success and non-zero is for failure.