简体   繁体   English

WPF线程和自定义Powershell主机-由于用户中断了命令,因此命令执行停止

[英]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. 我有一个带有Powershell自定义主机的WPF应用,该主机运行一个脚本,该脚本存储在嵌入式资源中。 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: 当我第一次添加此功能时,脚本可以完美运行,但是直到完成后我才看到WPF ui,因此我将以下内容添加到了主窗口视图模型的启动中:

        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. BackgroundWorker在UI线程上运行Completed代码。 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. 作为一个侧面或相关问题,当进程正在运行时,我也有通过数据绑定更新ui的问题。 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. 例如,我在状态栏中有一个状态指示器,可以在IDLE和BUSY之间切换,但是直到为时已晚,这似乎永远不会改变。 The binding is working fine but it's like I need the old VB DoEvents() to be called. 绑定工作正常,但就像我需要调用旧的VB DoEvents()一样。

Cheers 干杯

Have solved this after a coffee and a few minutes debugging. 喝咖啡和调试几分钟后解决了这个问题。

There erorr was because I hadn't implemented the PromptForChoice method fully. 出现错误是因为我没有完全实现PromptForChoice方法。

However, I still have problems with updating bindings. 但是,更新绑定仍然存在问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM