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

如果您LaunchProcess从 UI 线程调用,它显然会在myProcess.WaitForExit().

您可以简单地从启动方法中删除myProcess.WaitForExit()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 回答