Click here to Skip to main content
15,897,334 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello there I have a program which opens a process to carry out some calculations and then without waiting it will open another process of the same with different calculations but I only wish for 5 processes at a time to be open and then if there are 5 to wait until the process before it has closed to begin a new process..

here is what i have so far.. why does this not work and how might it be done better?

VB
Dim RunProcess As Integer = process.GetProcessesByName("run").Length
       Dim RunConhost As Integer = process.GetProcessesByName("conhost").Length
       For Each p In process.GetProcessesByName(RunConhost, RunProcess)
           If p.ProcessName.Length > 5 Then
               process.WaitForExit()
               process.Close()
           Else
               process.Start()
               Dim output As String = process.StandardOutput.ReadToEnd()
               RichTextBox1.Text = output
               process.WaitForExit()
               process.Close()
           End If
       Next



also i get a strange error part way through my code running
Couldn't connect to remote machine.
I am not sure why this error happens as I am not trying to connect to anyone or anything outside of my program?

thank you in advance!!
Posted
Updated 2-May-13 15:40pm
v2

1 solution

The whole thing is .... well ....

First, you're calling Process.GetProcessesByName twice. Why? You only need to do it once. Once you have the list, you can do whatever you want with it.

Now, look at that If statement expression. You're comparing the length of the name of a process to 5... You're not comparing the number of processes, you're comparing the LENGTH of the NAME of a process to 5. Do you see something wrong with that?

Then there's a For Each loop you don't need. Why are you enumerating a list of processes?? You just need to know how many of them are running.

I simple method would be to poll for the number of processes. If there are 5 processes already running, sleep for a few seconds, then go back and check the processes list again. Once there are less than 5 processes, you can start a new one and return to the caller.

It's not exactly the way I'd do it in production code, but YOU also have to support this code and maintain it. With your skill level, the simpler the code, the better off you are.
 
Share this answer
 
Comments
Dale 2012 3-May-13 3:10am    
thank you for helping me to grasp this better it is completely foolish now that i see it this way.
Dave Kreskowiak 3-May-13 7:13am    
It happens to all of us sooner or later.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900