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)
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 setProcess.EnableRaisingEventstotrue, and handle theExitedevent. I’m not sure if theExitedevent 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 likeInterlocked.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 forthis.InvokeRequiredbefore doing the actual work.