调度程序计时器未运行

时间:2012-10-09 11:29:49

标签: c# wpf

我正在尝试从我的WPF应用程序执行一些Python脚本。脚本正在生成日志文件,Tick事件中的代码正在读取它们并将其显示在文本框中。

我的问题是,LaunchProcess成功触发,但UI冻结。我有一个无限期的进度条,它也没有开始动画。我是WPF的初学者,为了让这段代码正常工作,我需要做一些非常小的工作。我没有收到任何错误/警告。脚本运行正常,最后我也知道了结果。但在运行期间,我的应用程序的用户界面冻结了。

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

    } 

2 个答案:

答案 0 :(得分:2)

如果您从UI线程中调用LaunchProcess,则显然会在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
}

答案 1 :(得分:1)

异步调用LaunchProcess方法将解决您的UI冻结问题

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

       Thread thread = new Thread(displayContentHandler);
       thread.IsBackground = true;
       thread.Start();
 }
相关问题