The program I made throws off threads which start executables through processes. Each thread runs the method below which starts the process and redirects the processes’ output into a string (info.additional which is per process).
I’ve learned through a lot of trial and error that I have to redirect output a certain way (using events for output received) or else the streams deadlock.
Now, I’m running on a dual core machine and if I have two threads running and starting processes, the program runs great and captures the output of these processes perfectly. But when I move to more than two threads (more threads than I have cores) the output sometimes doesn’t get captured for these. Why is this? And does anyone know how to fix this?
Thank you, Joe
public void ThreadProc()
{
info.watch.Start();
proc = InstantiateProcess();
proc.Start();
proc.BeginOutputReadLine();
}
private void procOutputDataReceived(object sendingProcess, DataReceivedEventArgs e)
{
if (e.Data != null)
{
//Console.WriteLine(System.Threading.Thread.CurrentThread.ManagedThreadId);
this.info.additional += e.Data.ToString();
}
}
Not sure if this could be causing the problem, but based on the code here your
ThreadProcis exiting before theProcessobject is done. According to the OutputDataReceived docs, you should be callingWaitForExit()“The application that is processing the asynchronous output should call the WaitForExit() method to ensure that the output buffer has been flushed.” Based on that line I would assume you could lose some data if you are not doing that.On an unrelated note,
Processobjects implement IDisposable so I would recommend changing your ThreadProc method like this (note I don’t see a declaration of proc here, but as you say you launch multiple processes I am assuming that proc is not a member variable and the declaration was just left out in the sample):