2

Each time CurlFile() is called it creates a new object from the ProcessObject class. The ProcessObject object starts the new Process. I want the Process.Exit event in each object to trigger the static event handler in the parent class, but it doesn't seem to be working for some reason.

class Curl
{
    StringContainer oStrings = new StringContainer();
    private static int _counter = 0;
    private string _curl;

    public Curl()
    {
        //oStartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
        _curl = oStrings.Curl;
    }

    public void CurlFile(string _arg)
    {
        ProcessObject oProcessObject = new ProcessObject(_arg, _curl);
    }

    private static void oProcess_Exited(object sender, EventArgs e)
    {
        _counter++;
        if (_counter == 1000)
        {
            MessageBox.Show("here");
        }
    }

    private class ProcessObject
    {
        ProcessStartInfo oStartInfo = new ProcessStartInfo();
        Process oProcess = new Process();

        public ProcessObject(string _arg, string _curl)
        {
            oStartInfo.FileName = _curl;
            oStartInfo.Arguments = _arg;
            oProcess.EnableRaisingEvents = true;
            oProcess.Exited += new EventHandler(oProcess_Exited);
            oProcess = Process.Start(oStartInfo);
        }
    }
}
Yahia
  • 69,653
  • 9
  • 115
  • 144
user1172282
  • 527
  • 1
  • 9
  • 19
  • 1
    Your objects are getting collected by the garbage collector. Also, the design, er... –  Feb 02 '12 at 17:15

1 Answers1

1

First off, as @Will mentions, you need to keep a reference to your Process objects so they don't get GC'd. Something like this (code untested):

class Curl
{
    internal static List<ProcessObject> _processes = new List<ProcessObject>();

    // ....

    private static void oProcess_Exited(object sender, EventArgs e)
    {
        var p = sender as Process;
        if (p != null && _processes.Contains(p))
           _processes.Remove(p);

        _counter++;
        if (_counter == 1000)
        {
            MessageBox.Show("here");
        }
    }

    public ProcessObject(string _arg, string _curl)
    {
        oStartInfo.FileName = _curl;
        oStartInfo.Arguments = _arg;
        oStartInfo.UseShellExecute = false;
        oProcess.EnableRaisingEvents = true;
        oProcess.Exited += new EventHandler(oProcess_Exited);
        oProcess = Process.Start(oStartInfo);
        Curl._processes.Add(oProcess);
    }
}

Also, as some people have found, the Process class can be spotty about detecting the Exit. I don't know if this holds true for the Exited event also, but I'd watch out for it and explicitly set UseShellExecute = false; as I have above.

Community
  • 1
  • 1
Chris Shain
  • 50,833
  • 6
  • 93
  • 125