Rohan
Rohan

Reputation: 1805

Dispatcher timer not running

I'm trying to execute some Python scripts from my WPF app. The scripts are generating the log files and the code in the Tick event is reading them and displaying that as it is in a textbox.

My issue here is that, that LaunchProcess fires successfully, but the UI freezes. I have an indefinite progress bar, which too does not start animating. I'm a beginner with WPF and there is something very small I have to do to get this code working. I'm not getting any error/warnings. The scripts run fine and in the end I get to know the result too. But during the run, the UI of my app freezes.

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

    } 

Upvotes: 0

Views: 944

Answers (2)

Ehsan Aleem Avee
Ehsan Aleem Avee

Reputation: 231

Calling LaunchProcess method asynchronously would resolve your UI Freeze Issue

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

       Thread thread = new Thread(displayContentHandler);
       thread.IsBackground = true;
       thread.Start();
 }

Upvotes: 1

Clemens
Clemens

Reputation: 128061

In case you call LaunchProcess from the UI thread it will obviously be blocked at myProcess.WaitForExit().

You might simply remove the myProcess.WaitForExit() and dispatcherTimer.Stop() calls from the launch method and check if the process is still running in the timer Tick handler.

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

    ... // all your code
}

Upvotes: 2

Related Questions