© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3mo ago•
31 replies
sharaths21312

Running many child processes in parallel

beginner
Part of my program creates a bunch of child processes and has them run in the background, roughly as follows
class Runner : IDisposable {
  ConcurrentBag<Process> Processes = []
  ...
  void Execute() {
    ...
    Parallel.ForEach(items, item => {
      string Command = ...
      Processes.Add(Process.Start(Command));
    }
  }
  ...
  void Dispose() {
    foreach (var p in Processes) {
      // ?
    }
  }
}
class Runner : IDisposable {
  ConcurrentBag<Process> Processes = []
  ...
  void Execute() {
    ...
    Parallel.ForEach(items, item => {
      string Command = ...
      Processes.Add(Process.Start(Command));
    }
  }
  ...
  void Dispose() {
    foreach (var p in Processes) {
      // ?
    }
  }
}

I want to know
- What's the proper way to dispose currently running processes in case it's needed (can I simply try
p.Kill()
p.Kill()
followed by
p.Dispose()
p.Dispose()
?)
- Is there a way to limit the number of threads spawned by foreach in this scenario?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

❔ Enumerating Running Processes
C#CC# / help
3y ago
✅ Xunit v3 running test cases in parallel
C#CC# / help
4mo ago
❔ send socket to UNRELATED process (NO FORKING, NO CHILD PROCESSES)
C#CC# / help
4y ago
❔ Processes and Async
C#CC# / help
3y ago