2017-12-15 2 views
1

원래 BusyIndicator를 구현할 계획이 없었지만 Powershell 스크립트를 실행하는 데 시간이 걸리므로 사용자와 혼동을 일으킬 수 있음을 알았습니다. C#에서 Powershell 스크립트로 BusyIndicator를 활용하는 방법을 보여주는 자습서가없는 것 같습니다. This은 WPF Extended Toolkit 작성자가 작성한 훌륭한 튜토리얼이지만 필요한 것은 아닙니다.WPF 응용 프로그램에서 BusyIndicator를 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); 
     } 
    } 
} 

나는 이미 BusyIndicator 내 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

글쎄, 나는 내 컴퓨터로 돌아 왔고, 슬프게도 BusyIndicator는 결코 멈추지 않는다. 메시지 상자의 if/else 문을 포함하여 "여기 나머지 코드"라고 말한 곳에 foreach (PSObject) 루프를 추가했습니다. 지표는 계속 가고 있습니다. – IRGeekSauce

+0

업데이트 후 전체 코드를 게시하십시오. IsBusy = false 부분을 놓치지 않았습니까? – Evk

관련 문제