-4

Alright I will attempt to explain every aspect of why I need to do this a certain way. Basically, I need an application to execute a certain .exe multiple times asynchronously. Specs:

  • I need to be able to restrict the amount of executions going at one time.
  • It has to use threading because my program has a GUI and simply launching the .exe's and monitoring them will lock up the .GUI AND the console for other things.

How should I go about doing this? (examples help me a lot)

Joey Gfd
  • 499
  • 1
  • 9
  • 15
  • 1
    -1 for being very incomplete (despite the 1st paragraph). And I think we saw this question before. What kind of interaction is there between the Main app and the worker apps? How much and what kind of I/O is involved? – H H Jul 12 '11 at 16:35
  • 3
    You said "*It has to use threading because my program has a GUI and simply launching the .exe's and monitoring them will lock up the GUI*" But the conclusion does not logically follow from the premise. **Why do you believe you have to use threading for this**? You are already launching multiple *processes* which already will have their own threads; why does the launcher need to have multiple threads? **Launching and monitoring a process is not blocking**. – Eric Lippert Jul 12 '11 at 17:46
  • It will lock up the console to do that – Joey Gfd Jul 13 '11 at 13:45

2 Answers2

9

I've already told you multiple times how you should go about this. The launcher program has a single thread. It monitors the child processes. If a process ends and there is a free processor, it starts up a new process and affinitizes the process to that processor. When it's not doing any of those things it yields control back to its UI. Since each of those operations is of short duration, the UI never appears to block.

Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067
2

UPDATE
Actually this wasn't a great answer. As Henk pointed out in my comments, when you call Process.Start() that's not a blocking call. You have to explicitly set Process.EnableRaisingEvents to true, and handle the Exited event. I'm not sure if the Exited event is fired in the calling thread (I doubt it, but you should check), but the point is starting a process isn't a blocking call, so you don't need more threads doing the waiting.

See this similar answer for more details: Async process start and wait for it to finish

PREVIOUS ANSWER
Fire off your threads (limited to your max number of threads), and have them run the external exe using the Process.Start() method. Make sure you set them to wait for the process to finish. When the processes finish, have the threads use something like Interlocked.Increment() to increment a counter variable that you can read from your main form code. Better still, have those threads call a callback delegate (e.g. Action<T>), which will in turn check for this.InvokeRequired before doing the actual work.

Community
  • 1
  • 1
Neil Barnwell
  • 41,080
  • 29
  • 148
  • 220
  • @Henk I was thinking about that, but couldn't remember what the mechanism was to be notified when a `Process` has completed. I've just looked up it and will update my answer. – Neil Barnwell Jul 12 '11 at 16:41
  • If you're going to wait, just use `Process.WaitForExit` - but it's a waste of threads. – configurator Jul 14 '11 at 23:37