Click here to Skip to main content
15,883,705 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
I have a problem when trying to control the number of processes started by another thread in my application. The task manager shows that there are 169 + processes started once my application starts scanning the file system. Of course this is to many processes for many computers to handle due to freezing the application or windows itself.

My solution is to only allow 10 processes from my application to start and if more or equal to that many processes with the same name are found to wait until the process has exited and then free the resources to allow the next process to start again if the process count is still below or equal to 10.

really all that I look to accomplish is a way to traffic the flow of processes started and stopped but cannot get it to work the way i want it to.

My application opens a CMD window with the arguments passed to it so that it executes the program in question with the string provided and then outputs that information back to the program for calculation.

here is the code I am working with..

Public Sub ThreadDis()
        DisProc = New Threading.Thread(AddressOf Dissassemble)
    End Sub

 Public Sub Dissassemble()
        Dim process1 As New Process
        Dim DisAmpath As String = """" & Application.StartupPath & "\" & "run.exe" & """" & "  "
        Dim Arguments As String = """" & Label7.Text & """"
        Dim Fullargs As String = DisAmpath & Arguments
        TextBox2.Text = Fullargs
        process1.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
        process1.StartInfo.CreateNoWindow = True
        process1.StartInfo.UseShellExecute = False
        process1.StartInfo.RedirectStandardOutput = True
        process1.StartInfo.RedirectStandardError = True
        process1.StartInfo.FileName = Fullargs
        For Each prog As Process In Process.GetProcessesByName("run")
            If prog.ProcessName.Count > 10 Then
                Dim output As String = process1.StandardOutput.ReadToEnd()
                RichTextBox1.Text = output
            End If
    End Sub

'this part of the code calls ThreadDis within a for each statement for my recursive file scan. It only opens the process i wish if the file extension has been satisfied.

  If DisAsmEXTENSIONS.Contains(fi.Extension) = True Then
                    End If


Why are you doing a ForEach over the collection of processes returned by GetProcessesByName?? You don't need it!!

On the very next line, you are NOT comparing the number of processes returned by GetProcessesByName to 10, you're comparing the length of the Name of the process, i.e.: "run"!

You really just need to poll for the number of processes returned. If it's 10, then Thread.Sleep for a few seconds and do it again.

If it's under 10, start a new process...

Seriously, I would get a beginner book on VB.NET and work through it. There's a LOT you don't seem to know, especially how to follow the execution of your code using the debugger.
Share this answer
Dale 2012 8-May-13 18:06pm    
ok fair enough but how do you "poll" for processes? what is the syntax for that being as i cannot find a suitable way to count processes if they have the same name. A simple example would really help but I also know i have allot to learn.
Dale 2012 8-May-13 18:27pm    
thank you I was being blind and not reading everything but your solution also has helped and the issue is not resolved thank you!
Dave Kreskowiak 8-May-13 19:21pm    
Dim waiting as Boolean = True

While (waiting)
If Process.GetProcessesByName("run").Count < 10 Then
' Launch new process
waiting = false
Thread.Sleep(5000) ' Sleep for 5 seconds
End If
End While
You can easily manage all child processes you start from your application, if you pay attention that the method System.Diagnostic.Process.Start returns an instance of System.Diagnostic.Process: [^].

In your code, you just ignore the return value, but you can collect those references to child processes in some container (such as System.Collections.Generic.List<System.Diagnostic.Process>), and them count them, wait for process termination in some other thread, kill the process or… do whatever else you may want.

I would only note that not only having so many processes would be really bad (I agree with your), but the whole idea of the program architecture based on starting of several starting processes is very, very questionable. The processes are relatively heavy wait, and, importantly, the separate processes are well isolated, so you have very limited ways of controlling processes form another process. Threads of a single process are much more flexible, accessible and light weight (in separate Application Domains or in the same one, it depends on the purpose).

That said: perhaps, if you told us the ultimate purpose of all your processes, you could get much better advice.

Share this answer
Dale 2012 8-May-13 16:05pm    
ok I will share more about the purpose of this application. It is a malware classifier that I run from a dos window then output it back to my program. I had it running on another thread as you can see which allows the program to open more than one process at a time to analyze the files in question. When the process has reached the end of the file the process is then closed to then open the next process for calculation. The problem is that it of course does not know when to stop so many processes are opened which makes things very sticky. I like the ideas of a generic list can you give any solution to that or help me to see a better way?

thank you
Sergey Alexandrovich Kryukov 8-May-13 16:37pm    
I though I already gave you the solution. Do you need to keep some fixed amount of the process? Keep them in some collection (actually, it can better be a stack or a queue; I don't know what exact strategy of adding processes do you want), do with that whatever you want — you have all possible control over all your child processes.

If you only add processes as others are complete, you also need a notification thread. It would wait for each or some process for termination (the thread wait state will use zero CPU time if you use Process.WaitForExit) and, when the process has exited, you can signal the thread adding processes: it's the time to add some more. This "adding thread" will look through the already terminated processes, remove their references from the collection, and add appropriate number of new ones (as know the current count of the collection).

Basically, that's it.

But do you have the source code for these child processes. If you do, you 100% need to resort to threads instead of separate processes.

Dale 2012 8-May-13 18:26pm    
Thank you I have solved this issue your comment is well appreciated and has helped a great deal!
Sergey Alexandrovich Kryukov 8-May-13 18:52pm    
Great. You are welcome.
Good luck, call again.

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