473,473 Members | 1,807 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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.Diagnostics.Process runObj = new System.Diagnostics.Process();
runObj.StartInfo.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
Sep 18 '08 #1
5 1480
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.QueueUserWorkItem(state =>
{
Process.Start("notepad.exe").WaitForExit();
count.Release();
});
}

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

Marc
Sep 18 '08 #2
On Thu, 18 Sep 2008 05:02:03 -0700, Marc Gravell <ma**********@gmail.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.EnableRaisingEvents property to
true. It's false by default.

Pete
Sep 18 '08 #3
d'oh! Cheers Pete - I was having a slow moment ;-p
Sep 18 '08 #4
On Thu, 18 Sep 2008 14:42:56 -0700, Marc Gravell <ma**********@gmail.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. :)
Sep 18 '08 #5
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 ProcessStartInfo("notepad.exe");
proc.EnableRaisingEvents = 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.exe" each time ;-p

Marc
Sep 19 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Jean-David Beyer | last post by:
I have six hard drives (4 SCSI and 2 EIDE) on my main machine with parts of a database on each drive. The main index is on one SCSI drive all to itself. The main data are on the other three SCSI...
6
by: Ana | last post by:
Hi! I have problems with the following scenario: My application is developed using C# under .NET. It must run on all Windows versions starting from Windows 98. The user must open different...
1
by: david | last post by:
I have a simple csharp form with a 'Go' button on it. This event launches a external exe successfully but the form color becomes white and does not repaint while that external program runs,...
2
by: Peter Wood | last post by:
I am attempting to run an external process from within my web service, but nothing happens. No errors are thrown, nor are any eventlog messages written. If I call the same classes directly from a...
5
by: snicks | last post by:
I'm trying to exec a program external to my ASP.NET app using the following code. The external app is a VB.NET application. Dim sPPTOut As String sPPTOut = MDEPDirStr + sID + ".ppt" Dim p As...
8
by: ToMasz | last post by:
There is a script running continuously which executes an external script in a loop using os.spawnlp(os.P_WAIT, 'extscript.py') function. After several hundred executions (cycles of the loop), the...
2
by: =?Utf-8?B?SXJmYW4=?= | last post by:
Hello, It may be a repeated question but I don't find the solution to the situation that I encounter in it. My application is monitoring another application that is built in VB6. The...
1
by: webotronics | last post by:
Hi, I need to add a timeout for external programs, as the external program sometimes never dies (it's a ClearQuest multisite call to the shipping server, that sometimes never ends, but simply...
2
by: Steve | last post by:
Hi All, I've been trying to come up with a good way to run a certain process at a timed interval (say every 5 mins) using the SLEEP command and a semaphore flag. The basic thread loop was always...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.