2017-12-15 9 views
1

BusyIndi​​catorを実装する予定はありませんでしたが、Powershellスクリプトの実行に時間がかかることがわかり、ユーザーと混乱する可能性があります。 BusyIndi​​catorをPowerShellスクリプトでC#で活用する方法を示すチュートリアルはありません。 Thisは、WPF Extended Toolkitの作成者によって書かれたので、素晴らしいチュートリアルですが、必要なものではありません。WPFアプリケーションでBusyIndi​​catorをPowershellスクリプトの実行で使用する方法

これは私が持っているものです。

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); 
     } 
    } 
} 

私はすでにBusyIndi​​catorは私のXAMLで設定しています:コードを簡潔にするために切り捨てられること

<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 

私は非常にこれに慣れています。どんな助けでも大歓迎です。

答えて

1

あなたは(Task.Runまたは類似の構築物で)serapateスレッド上でコードを実行、またはこのよう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); 
} 
+0

。手伝ってくれてどうもありがとう。それがうまくいくなら私は喜んでそれを受け入れるでしょう。 – IRGeekSauce

+0

さて、私は私のマシンに戻っています。そして悲しいことに、BusyIndi​​catorは決して止まらないのです。私はメッセージボックスのif/elseステートメントを含め、あなたの "残りのコードはここ"と言ったところで私のforeach(PSObject)ループを追加しました。インジケータはちょうど行くと行く。 – IRGeekSauce

+0

更新後に完全なコードを投稿してください。あなたはIsBusy =偽の部分を見逃しませんでしたか? – Evk

関連する問題