Can I read text from console application during console still runing process? - c#

I use ProcessStartInfo to run a console aplication and ProcessStartInfo can read text from the console after the console is closed:
using (Process p = Process.Start(st))
{
//Thread.Sleep(2000);
p.WaitForExit();
using (StreamReader rd = p.StandardOutput)
{
result = rd.ReadToEnd();
p.Close();
String result1 = String.Copy(result);
}
Is there another method to read text from the console while it is open?

You can use the OutputDataReceived of the Process class
string result = string.Empty;
using (Process process = new Process())
{
process.StartInfo = st; // your ProcessStartInfo
StringBuilder resultBuilder = new StringBuilder();
process.OutputDataReceived += (sender, e) =>
{
st.AppendLine(e.Data);
};
process.Start();
process.BeginOutputReadLine();
process.WaitForExit();
result = resultBuilder.ToString();
}
You simply add an event handler to the Process's OutputDataReceived event that gets called whenever the process outputs a line.
Then you need to call BeginOutputReadLine() after the process has been started to begin receiving those events.
In this example, I still wait for the process to exit just to complete the code. Of course you don't need to wait, the events occure while the process is running. So you can store your process variable in a member and dispose it later or even subscribe to its Exited event to get informed when the process terminates.

Related

C# check if method executed [duplicate]

This question already has answers here:
Wait until a process ends
(9 answers)
Wait for process to finish and then display message (C#)
(3 answers)
Closed 5 years ago.
I have a method which calls external exe file.
private void MainTask()
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "test.exe";
process.StartInfo = startInfo;
process.Start();
}
It can take 10-50-150 seconds to run test.exe file. The question is how can I check where it was executed or is still running.
I was trying to implement backgroundWorker like this:
private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
MainTask();
}
private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
pictureBoxOutput.Image =
MessageBox.Show("C Sharp is awesome.");
}
Is it possible to check when method is completely executed? Because right now I get a message "C Sharp is awesome." even though process is still running.
Edit: I want to create a progress bar and I need to update few elements on my form after process finishes. Process creates new files and I need to display new files' names. Of course it should finish first otherwise application will crash
You need to call process.WaitForExit()
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "test.exe";
process.StartInfo = startInfo;
process.Start();
process.WaitForExit(); // this will block while the process is running
It all boils down to having a callback function after the the programm is finished. There are two ways for this:
Register the Process classes Exited Event.
If you want to keep your current BackgroundWorker code, you have to add a "wait for the Process to finish befor you continue" call inside the BackgroundWorker, as Mihail Shishkov showed in his answer.
Process.Start() will not wait for the process to exit. It will continue instantly. Effectively using Process.Start() is the same as hitting the "Ok" button on the Windows Run Dialog. Your code will instantly continue, regardless what the programm does from then on. In most cases you even have the Reference to the process instance run out of scope instantly after calling start.
You can use the Exited event
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "test.exe";
process.StartInfo = startInfo;
process.Exited += (sender, e) => {
pictureBoxOutput.Image = ...;
MessageBox.Show("C Sharp is awesome.");
};
process.Start();

How to set Maximum method execution time

I've a method that contains a process that must be stopped in a deadline( ex: 3 seconds) whether it has finished or not, and I don't want to wait if it has finished executing before reaching that dead line.
using Process.WaitForExit(3000) makes the program wait 3s even if the process has stopped before reaching the limit.
One more thing, I'm using process.StandardOutput.ReadToEnd(); to read the execution result, I don't care if it returns null or empty string or whatever if it doesn't finish.
And I guess that timers will cause the same problem.
Any Ideas?
Exited event of your process can be handled for detecting exit time.
WaitForExit returns a Boolean value that indicates your process has reached the timeout before exit or not.
Test this code:
Process proc = new Process();
ProcessStartInfo procInfo = new ProcessStartInfo()
{
FileName = "d:/test.exe",
UseShellExecute = false,
RedirectStandardOutput = true
};
proc.StartInfo = procInfo;
proc.EnableRaisingEvents = true;
proc.Exited += (o, args) =>
{
MessageBox.Show(proc.StandardOutput.ReadToEnd());
};
proc.Start();
if (proc.WaitForExit(3000))
{
MessageBox.Show("YES");
}
else
{
MessageBox.Show("NO");
}

Send command to stop other process in C#

In my WPF application, I starts new process which runs batch file.
code is as follows
public void startProcess(string batchFileName)
{
Process proc = new Process();
//command to execute
proc.StartInfo.FileName = batchFileName;
proc.StartInfo.UseShellExecute = false;
// set up output redirection
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.EnableRaisingEvents = true;
proc.StartInfo.CreateNoWindow = true;
// see below handler
proc.ErrorDataReceived += proc_DataReceived;
proc.OutputDataReceived += proc_DataReceived;
//start a process
proc.Start();
proc.BeginErrorReadLine();
proc.BeginOutputReadLine();
waitThread = new Thread(new ThreadStart(WaitForProcess));
waitThread.Start();
}
void proc_DataReceived(object sender, DataReceivedEventArgs e)
{
// output will be in string e.Data
if (e.Data != null)
{
logsTextBox.Dispatcher.BeginInvoke(new SetLogText(UpdateText), DispatcherPriority.Normal, e.Data);
}
}
private void WaitForProcess()
{
proc.WaitForExit();
proc.Close();
}
Now I want to stop this newly created process in between. How to achieve that?
Any suggestions/pointers.
How to stop a running process
Close method can be used to stop a process
proc.Close();
CloseMainWindow can be used to request a stop
proc.CloseMainWindow();
Kill can be used to destroy the process
proc.Kill();
Remarks
Kill forces a termination of the process, while CloseMainWindow only requests a termination. When a process with a graphical interface is executing, its message loop is in a wait state. The message loop executes every time a Windows message is sent to the process by the operating system. Calling CloseMainWindow sends a request to close to the main window, which, in a well-formed application, closes child windows and revokes all running message loops for the application. The request to exit the process by calling CloseMainWindow does not force the application to quit. The application can ask for user verification before quitting, or it can refuse to quit. To force the application to quit, use the Kill method. The behavior of CloseMainWindow is identical to that of a user closing an application's main window using the system menu. Therefore, the request to exit the process by closing the main window does not force the application to quit immediately.
more info here
http://msdn.microsoft.com/en-us/library/vstudio/system.diagnostics.process.close
http://msdn.microsoft.com/en-us/library/vstudio/system.diagnostics.process.closemainwindow
http://msdn.microsoft.com/en-us/library/vstudio/system.diagnostics.process.kill

c# ProcessStartInfo.Start - reading output but with a timeout

If you want to start another process and wait (with time out) to finish you can use the following (from MSDN).
//Set a time-out value.
int timeOut=5000;
//Get path to system folder.
string sysFolder=
Environment.GetFolderPath(Environment.SpecialFolder.System);
//Create a new process info structure.
ProcessStartInfo pInfo = new ProcessStartInfo();
//Set file name to open.
pInfo.FileName = sysFolder + #"\eula.txt";
//Start the process.
Process p = Process.Start(pInfo);
//Wait for window to finish loading.
p.WaitForInputIdle();
//Wait for the process to exit or time out.
p.WaitForExit(timeOut);
//Check to see if the process is still running.
if (p.HasExited == false)
//Process is still running.
//Test to see if the process is hung up.
if (p.Responding)
//Process was responding; close the main window.
p.CloseMainWindow();
else
//Process was not responding; force the process to close.
p.Kill();
MessageBox.Show("Code continuing...");
If you want to start another process and read its output then you can use the following pattern (from SO)
// Start the child process.
Process p = new Process();
// Redirect the output stream of the child process.
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "Write500Lines.exe";
p.Start();
// Do not wait for the child process to exit before
// reading to the end of its redirected stream.
// p.WaitForExit();
// Read the output stream first and then wait.
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
How can you combine the two to read all input, not get stuck in deadlock and have a timeout if the running process goes awry?
This technique will hang if the output buffer is filled with more that 4KB of data. A more foolproof method is to register delegates to be notified when something is written to the output stream. I've already suggested this method before in another post:
ProcessStartInfo processInfo = new ProcessStartInfo("Write500Lines.exe");
processInfo.ErrorDialog = false;
processInfo.UseShellExecute = false;
processInfo.RedirectStandardOutput = true;
processInfo.RedirectStandardError = true;
Process proc = Process.Start(processInfo);
// You can pass any delegate that matches the appropriate
// signature to ErrorDataReceived and OutputDataReceived
proc.ErrorDataReceived += (sender, errorLine) => { if (errorLine.Data != null) Trace.WriteLine(errorLine.Data); };
proc.OutputDataReceived += (sender, outputLine) => { if (outputLine.Data != null) Trace.WriteLine(outputLine.Data); };
proc.BeginErrorReadLine();
proc.BeginOutputReadLine();
proc.WaitForExit();
You don't have to combine the two - the Process class has an event that fires when output is sent to the StandardOutput - OutputDataReceived.
If you subscribe to the event, you will be able to read output as it arrives and in your main program loop you can still timeout.
you can try modifying the first method to something like this
Process p = Process.Start(pInfo);
string output = string.Empty;
Thread t = new Thread(() => output = p.StandardOutput.ReadToEnd() );
t.Start();
//Wait for window to finish loading.
p.WaitForInputIdle();
//Wait for the process to exit or time out.
p.WaitForExit(timeOut);
void OpenWithStartInfo()
{
ProcessStartInfo startInfo = new ProcessStartInfo("IExplore.exe", "Default2.aspx");
startInfo.WindowStyle = ProcessWindowStyle.Minimized;
Process p = Process.Start(startInfo);
p.WaitForInputIdle();
//p.WaitForExit(2);
p.Kill();
}
You could also use the APM, like this:
Define a delegate for the ReadToEnd call:
private delegate string ReadToEndDelegate();
Then use the delegate to call the method like this:
ReadToEndDelegate asyncCall = reader.ReadToEnd;
IAsyncResult asyncResult = asyncCall.BeginInvoke(null, null);
asyncResult.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(10));
asyncCall.EndInvoke(asyncResult);
EDIT: Error handling removed for clarity.
Just add everything from the first example below the WaitForExit() call to the second example.
None of the above answers work for me when dealing with interactive promts. (My command sometimes promts a question to the user and that should also be covered by timeout).
This is my solution.
A disadvantage is that i don't get any output if we run in a timeout.
ReadToEnd() blocks the execution so we have to run it in another thread and kill this thread if the process runs into the specified timeout.
public static Tuple<string, string> ExecuteCommand(string command)
{
// prepare start info
var procStartInfo = new ProcessStartInfo("cmd", "/c " + command)
{
ErrorDialog = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
WorkingDirectory = #"C:\",
CreateNoWindow = true
};
// start process
var proc = new Process {StartInfo = procStartInfo};
proc.Start();
var error = "";
var output = "";
// read stdout and stderr in new thread because it is blocking
Thread readerThread = new(() =>
{
try
{
error = proc.StandardError.ReadToEnd().Trim();
output = proc.StandardOutput.ReadToEnd().Trim();
}
catch (ThreadInterruptedException e)
{
Debug.WriteLine("Interrupted!!");
}
});
readerThread.Start();
// wait for max 6 seconds
if (proc.WaitForExit(6_000))
{
// if command runs to an enc => wait for readerThread to collect error/output stream
readerThread.Join();
}
else
{
// if process takes longer than 6 seconds => kill reader thread and set error to timeout
readerThread.Interrupt();
error = "Timeout!";
}
// return output and error
return new Tuple<string, string>(output, error);
}

Periodically check command line output in C#

In my C# program I call an external program from command line using a process and redirecting the standard input. After I issue the command to the external program, every 3 seconds I have to issue another command to check the status - the program will respond with InProgress or Finished. I would like some help in doing this as efficient as possible. The process checks the status of a long running operation executed by a windows service (for reasons I would not like to detail I cannot interact directly with the service), therefore after each command the process exits, but the exitcode is always the same no matter the outcome.
use Process.Exited event and Process.ExitCode property
For examples:
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.exited.aspx
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.exitcode.aspx
You can use a Timer to execute some code at a specified interval, and Process.StandardOutput to read the output of the process:
Timer timer = new Timer(_ =>
{
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "foobar.exe";
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
switch (output)
{
case "InProgress":
// ...
break;
case "Finished":
// ...
break;
}
});
timer.Change(TimeSpan.FromSeconds(0), TimeSpan.FromSeconds(3));

Categories