wpfpowershellpowershell-sdk

WPF threading and custom Powershell host - Command execution stopped because the user interrupted the command


I have a WPF app with a powershell custom host that runs a script that is stored in an embedded resource. After loading, the user click the execute button and the script is Invoked but I see the following error:

Command execution stopped because the user interrupted the command

The user (ie, me) did nothing to interupt the script so I'm guessing that this is some sort of threading issue.

The application also accepts a command line parameter that tells the script to execute immediately. When I first added this feature, the script ran perfectly, but I didnt see the WPF ui until this was completed so I added the following to the startup of the main window view model:

        var bgWork = new BackgroundWorker();
        bgWork.DoWork += (s,e) => { };
        bgWork.RunWorkerCompleted += (s,a) =>
        {
            var app = (Application.Current as App);

            if (app.ExecuteImmediate && StartCommand.CanExecute(null))
                StartCommand.Execute(null);

        };
        bgWork.RunWorkerAsync();

The BackgroundWorker runs the Completed code on the UI thread. This is just a little trick I use quite often and it works ok mostly.

So, a long question I know, but has anyone got any ideas what I'm doing wrong?

As a side or related issue, I also have problems updating the ui via data binding when a processes is running. Eg, I have a status indicator in the status bar that switches between IDLE and BUSY but this never seems to change until it's too late. The binding is working fine but it's like I need the old VB DoEvents() to be called.

Cheers


Solution

  • Have solved this after a coffee and a few minutes debugging.

    There erorr was because I hadn't implemented the PromptForChoice method fully.

    However, I still have problems with updating bindings.