Skip to content

ApplyPhase Class

Richard Martin edited this page Mar 25, 2024 · 2 revisions

Bootstrapper.Phases.ApplyPhase

The only thing of note that we're doing is notifying the UI that the install has completed. This class also handles the Error event, so we'll look at that, too.

OnApplyComplete method

The ApplyComplete event is raised once the install has been finalized. We handle the same cancel check that we did in the plan phase. We also handle the HRESULT like we did when completing the detect and plan phases. After doing that, we set the BA status to Applied so the UI knows the install was a success.

_model.State.BaStatus = BaStatus.Applied;

Finally, we either raise an event to let the UI know it should refresh or shut the BA down if the UI is passive or the install is running silently.

if (_model.State.Display == Display.Full)
    ApplyPhaseComplete?.Invoke(this, EventArgs.Empty);
else
    _model.UiFacade.ShutDown();

OnError method

The WiX engine will raise this event when it encounters an exception.

Important

The Error event doesn't replace checking HRESULT when handling events.

The first thing to check for is a cancel exception. WiX supplies a code for this. This error can occur when a request to cancel was received from any event that supports it. As was covered in the plan and apply phase tutorials, it's possible to receive a cancel HRESULT. Also handling this WiX error code will cover all your bases.

if (e.ErrorCode == ErrorHelper.CancelCode)
    _model.State.BaStatus = BaStatus.Cancelled;

Otherwise, we should log the error and display it in a message box if not running silently. WiX provides a hint about the buttons that are appropriate for a message box and, if it gave a hint, will handle the result that the user selects.

if (_model.UiFacade.IsUiShown)
{
    var button = MessageBoxButton.OK;
    var buttonHint = e.UIHint & 0xF;

    if (buttonHint >= 0 && buttonHint <= 4)
        button = (MessageBoxButton)buttonHint;

    var response = _model.UiFacade.ShowMessageBox(e.ErrorMessage, button, MessageBoxImage.Error, MessageBoxResult.None);

    if (buttonHint == (int)button)
    {
        e.Result = (Result)response;
        _model.Log.Write($"User response: {response}");
    }
}

Previous: PlanPhase Class || Next: CancelHandler Class

Clone this wiki locally