Linit the number of external process running at any one time

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jeff

    Linit the number of external process running at any one time

    I need to run many processes from my application. and I am using the
    following enclosed in a for next loop:

    System.Diagnost ics.Process runObj = new System.Diagnost ics.Process();
    runObj.StartInf o.FileName = exepath;
    runObj.Start();

    What I want to do is limit the number of of process which are run at any
    one time. So if I have 20 processes to run I can limit it to 10
    consecutive processes. When a process finishes the next in the list
    starts untill all process finish.

    How could I achieve this using this code or is there a better way of
    doing this.

    Regards
    Jeff
  • Marc Gravell

    #2
    Re: Linit the number of external process running at any one time

    Sounds like a semaphore might be the simplest answer:

    Semaphore count = new Semaphore(5, 5);
    for (int i = 0; i < 20; i++)
    {
    count.WaitOne() ;
    ThreadPool.Queu eUserWorkItem(s tate =>
    {
    Process.Start(" notepad.exe").W aitForExit();
    count.Release() ;
    });
    }

    I tried to use the Exited event (rather than ThreadPool), but it didn't
    seem to raise...

    Marc

    Comment

    • Peter Duniho

      #3
      Re: Linit the number of external process running at any one time

      On Thu, 18 Sep 2008 05:02:03 -0700, Marc Gravell <marc.gravell@g mail.com>
      wrote:
      [...]
      I tried to use the Exited event (rather than ThreadPool), but it didn't
      seem to raise...
      It won't, unless you set the Process.EnableR aisingEvents property to
      true. It's false by default.

      Pete

      Comment

      • Marc Gravell

        #4
        Re: Linit the number of external process running at any one time

        d'oh! Cheers Pete - I was having a slow moment ;-p

        Comment

        • Peter Duniho

          #5
          Re: Linit the number of external process running at any one time

          On Thu, 18 Sep 2008 14:42:56 -0700, Marc Gravell <marc.gravell@g mail.com>
          wrote:
          d'oh! Cheers Pete - I was having a slow moment ;-p
          Well, so far I think I've still had more "slow moments" than you. So
          you're ahead of the game. :)

          Comment

          • Marc Gravell

            #6
            Re: Linit the number of external process running at any one time

            For the OP's benefit - the same without the ThreadStart would be:

            Semaphore count = new Semaphore(5, 5);
            for (int i = 0; i < 20; i++)
            {
            count.WaitOne() ;

            Process proc = new Process();
            proc.StartInfo = new ProcessStartInf o("notepad.exe" );
            proc.EnableRais ingEvents = true;
            proc.Exited += delegate
            {
            count.Release() ;
            proc.Dispose();
            };
            proc.Start();
            }

            Obviously you might be picking successive commands out of an array /
            list, rather than using "notepad.ex e" each time ;-p

            Marc

            Comment

            Working...