Reputation: 1
I have a 3rd party exe that accepts the path to a single text file via user input and creates new text files after doing some calculations. It is normally run via the console and accepts inputs one at a time.
I wrote a Winform application to allow us to mass generate these input files based on some parameters, and then pass those input files in to the exe one at a time.
It seems to be working fairly well so far, if I have it generate 2 or 3 input files, the output files shortly appear in the correct folder. But if I generate and pass through 10 files (one at a time), the first 3 or so files appear correctly, but then the later ones do not.
I thought that it was that perhaps my program was outpacing the exe's calculations, so I put some pauses in between the console inputs. That didn't change anything, but I found out that once I close out of my winform, all of the late files appear in my folder all at once.
Any thoughts on what is happening here?
This is my first time using Process, so it's probably not ideal. I was working originally with not putting in the next input until the exe was at the correct line, but I was struggling with reading the output. Then I deleted what I had and wrote the simple bit below, not expecting that to be enough. Apparently it was and I actualy started getting results.
Thanks!
Process p = new Process();
ProcessStartInfo start = new ProcessStartInfo();
start.CreateNoWindow = true;
start.UseShellExecute = false;
start.FileName = @"pathtoexe";
start.WindowStyle = ProcessWindowStyle.Hidden;
start.WorkingDirectory = Path.GetDirectoryName(start.FileName);
start.RedirectStandardOutput = true;
start.RedirectStandardInput = true;
start.RedirectStandardError = true;
p.Start();
StreamWriter writer = p.StandardInput;
writer.WriteLine("9"); // exe asks where the dll is stored, 9 indicates the path we use
Thread.Sleep(1000);
writer.WriteLine("1"); // exe is asking which type of output files we want, 1 indicates the basic text output calculations
Thread.Sleep(1000);
for (int j = 1; j<= _mun.Count;j++)
{
writer.WriteLine(@"..\Inputs\GeneratedInputs_"+j+".in"); //The input that is read by one run of the exe. We create these files before getting to this step
Thread.Sleep(1000);
writer.WriteLine(""); // exe asks us to confirm by entering a character. Just hitting enter works
Thread.Sleep(1000);
writer.WriteLine("2"); //the exe asks us which calculations we are running. 2 is the result we want
Thread.Sleep(5000);
if (j == _mun.Count) //The last input of the exe is if we want to run another run again or exit. On the last item of our list, choose "exit" with 0 instead of run again, with 1.
{
writer.WriteLine("0");
// p.WaitForExit(); I'm not really sure where in all of this I should be putting a WaitForExit, if at all.
}
else
{
writer.WriteLine("1");
}
Thread.Sleep(1000);
}
Upvotes: 0
Views: 28
Reputation: 2738
You can call
writer.Flush()
To clear the buffer and ensure all data is written. You should also consider wrapping your writer instance in a using clause to ensure proper disposal
using (StreamWriter writer = writer = p.StandardInput)
{
writer.WriteLine("test");
}
Upvotes: 1