2016-08-22 6 views
0

ST-LINK_CLI.exeを使用してファームウェアをST-LINKにプログラムするアプリケーションがあります。プロセスの実行中にテキストを表示できません。C#WPF

ユーザがファームウェアを選択し、開始ボタンを押してプロセスが開始されます。しかし、ボードをプログラムするにはかなりの時間がかかり、ユーザーはプログラムがクラッシュしたと考えるかもしれません。テキストブロックに「ボードプログラミング...」を表示して、動作していることを知るようにします。

しかし、現時点では、プログラムは既にプログラムされていて、 。必ず、なぜ次は、[スタート]ボタンイベントに私のコードです:

ProcessStartInfo start = new ProcessStartInfo(); //new process start info 
     start.FileName = STPath; //set file name 
     start.Arguments = "-C -ME -p " + firmwareLocation + " -v -Run"; //set arguments 
     start.UseShellExecute = false; //set shell execute (need this to redirect output) 
     start.RedirectStandardOutput = true; //redirect output 
     start.RedirectStandardInput = true; //redirect input 
     start.WindowStyle = ProcessWindowStyle.Hidden; //hide window 
     start.CreateNoWindow = true; //create no window 


     using (Process process = Process.Start(start)) //create process 
     { 

      try 
      { 

       while (process.HasExited == false) //while open 
       { 
        process.StandardInput.WriteLine(); //send enter key 
        programmingTextBlock.Text = "Board Programming..."; 
       } 

       using (StreamReader reader = process.StandardOutput) //create stream reader 
       { 
        result = reader.ReadToEnd(); //read till end of process 
        File.WriteAllText("File.txt", result); //write to file 
       } 

      } 
      catch { } //so doesn't blow up 
      finally 
      { 
       int code = process.ExitCode; //get exit code 
       codee = code.ToString(); //set code to string 
       File.WriteAllText("Code.txt", codee); //save code 
       } 

は、プロセスの実行を開始またはプロセスが実行されている間、前に表示するテキストを取得するとにかくはあり

おかげ ?ルーシー

+0

UIを操作し、あなたが別のthredを使用する必要がある時に何かを行うには。 – Whencesoever

+0

どうすればこのことをやりますか? – lucycopp

答えて

3

問題whileループが実行されている間は、メインスレッドと同じように、UIは更新されません。これを解決する正しい方法は、DispatcherまたはBackground Workerを使用して、別のスレッドに「問題のある」コードを置くことです。

代わりに、あなたはwhileループの外このprogrammingTextBlock.Text = "Board Programming...";を取ると、この行を追加することができます。

Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, 
             new Action(delegate { })); 

これは、ループに入る前に、UIを「リフレッシュ」する必要があります。

+0

ありがとうございました! – lucycopp

+0

うれしいです:) – Pikoh

1

長時間の実行中に表示され、実行時に消えてしまうのは、Waiting Barです。あなたじゃない? これを行うには、async/awaitパターンを実装して、UIスレッドがスタックしないようにする必要があります。あなたのビューモデルで :

  this.IsBusy = true; 
      await MyTaskMethodAsync(); 
      this.IsBusy = false; 

MyTaskMethodAsync一方戻りTask。で あなたXAMLあなたBusy Barを定義し、IsBusy財産への結合あなたはC#コードで見ることができます:

<Border Visibility="{Binding IsBusy,Converter={converters:BooleanToSomethingConverter TrueValue='Visible', FalseValue='Collapsed'}}" 
      Background="#50000000" 
      Grid.Row="1"> 
     <TextBlock Foreground="White" 
        VerticalAlignment="Center" 
        HorizontalAlignment="Center" 
        Text="Loading. . ." 
        FontSize="16" /> 
    </Border> 
関連する問題