Process.Start crashes my WPF program

I have a WPF program that opens a Word document in progress and waits for the process to complete before continuing. If I leave Word open for a few hours, my program crashes.

I can see that the memory for my application is constantly growing while the process is running.

I tried two ways to do it and both have memory issues.

Path number 1

public void ShowExternalReference(string externalRef, bool waitForCompletion)
{
    if (!string.IsNullOrEmpty(externalRef))
    {
        using (var p = Process.Start(@externalRef))
        {
            if (waitForCompletion)
            {
                // Wait for the window to finish loading.
                p.WaitForInputIdle();

                // Wait for the process to end.
                p.WaitForExit();
            }
        }
    }
}

      

Path number 2

public void ShowExternalReference(string externalRef, bool waitForCompletion)
{
    if (!string.IsNullOrEmpty(externalRef))
    {
        using (var p = Process.Start(@externalRef))
        {
            if (waitForCompletion)
            {
                while (!p.HasExited)
                {
                    Thread.Sleep(1000);
                }
            }
        }
    }
}

      

Any ideas?

+3


source to share


1 answer


I read the comments and it seems to have been a memory problem with WaitForExit()

for a long time.

So, I would do something like this:

  • Start a process and get only its PID
  • Check regularly if the process is active

It might not cause the same memory problem.



My suggestion:

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{

    private System.Threading.Timer _timer;

    public MainWindow()
    {
        InitializeComponent();

        this.Content = new TextBlock() { Text = "Close notepad.exe when you want..." };

        // - Launch process
        Process p = Process.Start("notepad.exe");
        int processId = p.Id;
        _timer = new System.Threading.Timer(new System.Threading.TimerCallback(o => CheckPID((int)o)), processId, 0, 1000);
    }

    /// <summary>
    /// Check if Process has exited
    /// </summary>
    /// <remarks>This code is NOT in UI Thread</remarks>
    /// <param name="processId">Process unique ID</param>
    private void CheckPID(int processId)
    {
        bool stillExists = false;
        //Process p = Process.GetProcessById(processId); // - Raises an ArgumentException if process has alredy exited
        Process p = Process.GetProcesses().FirstOrDefault(ps => ps.Id == processId);
        if (p != null)
        {
            if (!p.HasExited)
                stillExists = true;
        }

        // - If process has exited, do remaining work and stop timer
        if (!stillExists)
        {
            _timer.Dispose();

            // - Ask UI thread to execute the final method
            Dispatcher.BeginInvoke(new Action(ExternalProcessEnd), null);
        }
    }


    /// <summary>
    /// The external process is terminated
    /// </summary>
    /// <remarks>Executed in UI Thread</remarks>
    private void ExternalProcessEnd()
    {
        MessageBox.Show("Process has ended");
    }

}

      

The downside is that we cannot get StandardOutput, StandardError and ExitStatus.

+1


source







All Articles