0

WPF アプリからいくつかの Python スクリプトを実行しようとしています。スクリプトはログ ファイルを生成し、Tick イベントのコードはそれらを読み取り、テキスト ボックスにそのまま表示します。

ここでの問題は、LaunchProcess が正常に起動するが、UI がフリーズすることです。私は無期限の進行状況バーを持っていますが、これもアニメーションを開始しません。私は WPF の初心者で、このコードを機能させるには非常に小さな作業が必要です。エラー/警告は表示されません。スクリプトは正常に実行され、最終的には結果もわかります。しかし、実行中にアプリの UI がフリーズします。

private void LaunchProcess(string paramStr)
        {

        Process myProcess = new Process();
        StartProgressBar();
        try
        {
            dispatcherTimer = new DispatcherTimer();
            dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
            dispatcherTimer.Interval = new TimeSpan(0, 0, 0);
            dispatcherTimer.Start();

            myProcess.StartInfo.UseShellExecute = false;
            // You can start any process
            myProcess.StartInfo.FileName = "C:\\Python32\\python.exe";
            myProcess.StartInfo.Arguments = "\""+paramStr+"\"";
            myProcess.StartInfo.CreateNoWindow = true;
            myProcess.StartInfo.RedirectStandardOutput = true;
            myProcess.StartInfo.RedirectStandardError = true;
            myProcess.Start();

            myProcess.WaitForExit();
            // This code assumes the process you are starting will terminate itself.  
            // Given that is is started without a window so you cannot terminate it  
            // on the desktop, it must terminate itself or you can do it programmatically 
            // from this application using the Kill method.
            dispatcherTimer.Stop();
        }
        catch
        {
            MessageBox.Show("Process Launch Failed!!", "Failure", MessageBoxButton.OK, MessageBoxImage.Error);
        }
    }

    private void dispatcherTimer_Tick(object sender, EventArgs e)
    {
        //txtOutPut.Text = "";
        txtOutPut.Text += "\n" + DateTime.Now.ToString();
        if (File.Exists(scriptPath+"\\log.txt"))
        {

            //File.Copy("C:\\FlashAuto\\Execution_Logs\\log.txt", "C:\\FlashAuto\\Temp\\log.txt", true);
            TextReader readLogs = new StreamReader(scriptPath + "\\log.txt");
            string line = readLogs.ReadLine();
            while (line != null)
            {
                txtOutPut.Text += "\n" + line;
                line = readLogs.ReadLine();
                txtOutPut.ScrollToEnd();
            }

            //CountLines = txtExecLog.LineCount - 1;
            readLogs.Close();
            // Forcing the CommandManager to raise the RequerySuggested event 
            txtOutPut.ScrollToEnd();
            CommandManager.InvalidateRequerySuggested();
            readLogs.Dispose();
        }
        else
        {
            txtOutPut.Text += "log file not found at: " + DateTime.Now.ToString();
        }

    } 
4

2 に答える 2

2

UI スレッドから呼び出した場合LaunchProcess、明らかに でブロックされmyProcess.WaitForExit()ます。

myProcess.WaitForExit()launch メソッドからとの呼び出しを単純に削除しdispatcherTimer.Stop()、プロセスがまだタイマー Tick ハンドラーで実行されているかどうかを確認することができます。

private void dispatcherTimer_Tick(object sender, EventArgs e) 
{ 
    if (myProcess.WaitForExit(0)) // check with timeout zero
    {
        dispatcherTimer.Stop();
    }

    ... // all your code
}
于 2012-10-09T11:45:31.880 に答える
1

LaunchProcessメソッドを非同期に呼び出すと、UI フリーズの問題が解決します

public void LaunchProcessAsynchrousCall(string paramStr) 
{
      ThreadStart displayContentHandler = delegate()
      {
           LaunchProcess(paramStr)
       };

       Thread thread = new Thread(displayContentHandler);
       thread.IsBackground = true;
       thread.Start();
 }
于 2012-10-09T12:11:49.477 に答える