如何在WPF应用程序中将BusyIndicator与Powershell脚本一起使用

IRGeek酱

我最初没有计划实现BusyIndi​​cator,但我意识到我的Powershell脚本需要花费一些时间来执行,这可能会引起与用户的混淆。似乎没有任何教程显示如何在C#中将BusyIndi​​cator与Powershell脚本一起使用。是一个很棒的教程,因为它是由WPF Extended Toolkit的作者编写的,但是它并不是我所需要的。

这就是我所拥有的。为了简洁起见,代码被截断了:

public partial class MainWindow : Window
{

    public MainWindow()
    {
         InitializeComponent();

    }

    private void imageBtn_Click(object sender, RoutedEventArgs e)
    {
        Button imgBtn = sender as Button;
        string appToCheck = GetSubString(imgBtn.Name);            

        switch(appToCheck)
        {
            case "weather":
               InvokePowerShell(appToCheck);
                break;
            case "news":
                //DO THE SAME FOR ALL CASES
                break;
                //17 more cases follow
        }
    }


  private string GetSubString(string buttonName)
  {
      int index = buttonName.IndexOf('B');
      return buttonName.Substring(0, index);
  }



  private void InvokePowerShell(string str)
    {
        str = char.ToUpper(str[0]) + str.Substring(1);

        PowerShell ps = PowerShell.Create();
        ps.AddScript("Get-AppxPackage | Select Name | Where-Object ($_.Name -like '*" + str + "*'}");

        _busyIndicator.IsBusy = true;
        ps.BeginInvoke<PSObject>(null, new PSInvocationSettings(), ar =>
        {
            try
            {
                var psOutput = ps.EndInvoke(ar);

                this.Dispatcher.Invoke(() => _busyIndicator.IsBusy = false);
                foreach (PSObject item in psOutput)
                {
                    if (item.Equals(String.Empty))
                    {
                        MessageBox.Show(str + " is not installed so cannot be removed.");
                    }
                    else
                    {
                        if (MessageBox.Show("This cannot be undone.\nContinue?", "Warning", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.No)
                        {
                            //DO NOTHING
                        }
                        else
                        {
                            //TODO Remove the app
                            MessageBox.Show(str + " successfully removed.");
                        }
                    }
                }
            }
            finally
            {
                //dispose of it
                ps.Dispose();
            }
        }, null);
      }
   }
}   

我已经在XAML中设置了BusyIndi​​cator:

<toolkit:BusyIndicator x:Name="_busyIndicator" IsBusy="False" BusyContent="One moment....">
<!-- Insert the rest of my markup here -->
</toolkit:BusyIndicator>

我还将有一个更长的方法来删除应用程序中列出的所有内容,因此,我一定会想要该指示器。我尝试遵循上面链接中给出的教程,但是我的foreach循环超出范围时遇到了问题。

我试图使用异步BeginInvoke()方法无济于事。繁忙指示器一直持续运行,如下所示:

PSDataCollection<PSObject> outputCollection = new PSDataCollection<PSObject>();
IAsyncResult result = PowerShellInstance.BeginInvoke<PSObject>(outputCollection);
while (result.IsCompleted == false)
        {
            _busyIndicator.IsBusy = true;
        }
//Then my foreach loop shown above with the if statements and MessageBox notifications

我对此很陌生。任何帮助将不胜感激。

EVK

您可以在锯齿状线程(具有Task.Run或类似构造)上运行代码,或使用BeginInvoke如下代码:

private void InvokePowerShell(string str) {
    str = char.ToUpper(str[0]) + str.Substring(1);
    // remove using, or move to a field
    PowerShell ps = PowerShell.Create();
    ps.AddScript("Get-AppxPackage | Select Name | Where-Object {$_.Name -like '*" + str + "*'}");
    //This is where the app pauses slightly and I need a busyindicator                
    _busyIndicator.IsBusy = true;
    ps.BeginInvoke<PSObject>(null, new PSInvocationSettings(), ar => {
        try {                    
            var psOutput = ps.EndInvoke(ar);
            // note, you are not on UI thread here
            this.Dispatcher.Invoke(() => _busyIndicator.IsBusy = false);
            // the rest of your code here
        }
        finally {
            // if did not move to a field - dispose here
            ps.Dispose();
        }
    }, null);
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在Marionette JS中将路由器与应用程序一起使用

如何在.NET Web API应用程序中将FluentValidation与Swagger一起使用?

如何在电子应用程序中将sqlite3与变量一起使用

如何在桌面应用程序中将DbContext与DI一起使用?

如何在我的 node.js 应用程序中将 nodemon 与 docker 一起使用?

如何在Trigger.io应用程序中将Angular视图模板与本地模板文件一起使用?

如何在Spring webflux应用程序中将Spring WebSessionIdResolver与Spring Security 5一起使用?

如何在Spring Boot应用程序中将千分尺与jmx一起使用?

如何在Angular 6应用程序中将Image Upload插件与CKEditor 4一起使用?

如何在Nginx中将WordPress与Django应用程序一起安装

将AppHarbour与WPF应用程序一起使用

如何使附属程序集在 PublishSingleFile 中与我的 WPF 应用程序一起使用

如何让 jQuery mobile 与应用程序脚本一起使用?

Shopify应用程序-将脚本标签与Ruby on Rails应用程序一起使用

如何在VSCode中将cmder与powershell一起使用?

蓝牙信标如何与应用程序一起使用?

如何在同一个 React 应用程序中将 redux 和 graphql 与 apollo 客户端一起使用?

如何在Tomcat 8中将HTML文件与Web应用程序一起部署?

如何在C#中将依赖注入与工作单元和存储库一起使用?(不是基于网络的应用程序)

无法在Rust / Actix应用程序中将计时功能与柴油一起使用

无法在Android中将Facebook本机应用程序与Gigya社交登录一起使用

在Windows应用程序中将Windows身份验证与Sql Server一起使用

在phoenix应用程序中将gettext与来自数据库的内容一起使用?

在JSX中将React与现有应用程序一起使用

在Shiny应用程序中将summarytools :: descr()与by()一起使用时,变量名消失

在Rails应用程序中将实例方法与Chartkick一起使用

在WinExist中将应用程序名称与空格一起使用

在角度应用程序中将Ui-select与多个类别一起使用

将WPF应用程序中的RESX文件与Prism一起使用