Capturing process output via OutputDataReceived event - c#

I'm trying to capture process output in "realtime" (while it's running). The code I use is rather simple (see below). For some strange reason the OutputDataReceived event is never called. Why?
private void button2_Click(object sender, EventArgs e)
{
// Setup the process start info
var processStartInfo = new ProcessStartInfo("ping.exe", "-t -n 3 192.168.100.1")
{
UseShellExecute = false,
RedirectStandardOutput = true
};
// Setup the process
mProcess = new Process { StartInfo = processStartInfo, EnableRaisingEvents = true };
// Register event
mProcess.OutputDataReceived += OnOutputDataReceived;
// Start process
mProcess.Start();
mProcess.WaitForExit();
}
void OnOutputDataReceived(object sender, DataReceivedEventArgs e)
{
//Never gets called...
}

You need to call
mProcess.BeginOutputReadLine();
BeginOutputReadLine - "Begins asynchronous read operations on the redirected StandardOutput stream of the application."

void ExecuteCommand(string cmdpath, string cmdargs)
{
string command = cmdpath + " " + cmdargs;
tabc_results.SelectTab(1);
DoConsole("\r\nCmd>> " + command + "\r\n");
var processInfo = new System.Diagnostics.ProcessStartInfo("cmd.exe", "/c " + command);
processInfo.CreateNoWindow = true;
processInfo.UseShellExecute = false;
processInfo.RedirectStandardError = true;
processInfo.RedirectStandardOutput = true;
var process = System.Diagnostics.Process.Start(processInfo);
process.OutputDataReceived += (
object sender, System.Diagnostics.DataReceivedEventArgs e
) => DoConsole("stdout>> " + e.Data + "\r\n");
//Console.WriteLine("output>>" + e.Data);
process.BeginOutputReadLine();
process.ErrorDataReceived += (
object sender, System.Diagnostics.DataReceivedEventArgs e
) =>DoConsole("stderr>> " + e.Data + "\r\n");
//Console.WriteLine("error>>" + e.Data);
process.BeginErrorReadLine();
process.WaitForExit();
DoConsole("retcode>> " + process.ExitCode.ToString() + "\r\n");
//Console.WriteLine("ExitCode: {0}", process.ExitCode);
process.Close();
}

Related

.NET standardOut is not getting redirected

I have a part of a code where I need to start up a console application from my .NET console application and get its output and in the best scenario also print that output. But I am getting error:
StandardOut has not been redirected or the process hasn't started yet
I got RedirectStandardOutput set to true and the thread has according to log started (I got "read started" printed out).
I am attaching problematic pieces of the code below. Anyone got an idea where the problem is? I will be grateful for any useful advice.
Thread readingThread = new Thread(Read);
ProcessStartInfo info = new ProcessStartInfo()
{
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true,
FileName = "../../" + path + "/server_run.sh",
};
info.RedirectStandardOutput = true;
info.UseShellExecute = false;
info.FileName = "../../" + path + "/server_run.sh";
using (Process server = Process.Start(info))
{
readingThread.Start(server);
Console.WriteLine("Starting the server");
int serverNumber = serverPaths.FindIndex(x => x == path);
Console.WriteLine(serverNumber);
serverNumbers.Insert(serverNumber, server.Id);
int i = 0;
foreach (int a in serverNumbers)
{
Console.WriteLine("index: " + i + " " + a);
i++;
}
//////////////string ProcessID;
Console.WriteLine(server.Id.ToString()); ////////////////
Console.WriteLine("Server started");
}
readingThread.Join();
private static void Read(object parameter)
{
Console.WriteLine("read started");
Process process = parameter as Process;
process.BeginOutputReadLine();
process.OutputDataReceived += new DataReceivedEventHandler(MyProcOutputHandler);
}
private static void MyProcOutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
{
// Collect the sort command output.
if (!String.IsNullOrEmpty(outLine.Data))
{
Console.WriteLine(outLine.Data);
}
}

How to get the the result of batch file to C#

I am currently struggling with bringing existing data in batch file to C# window form
The whole objective is getting a result lively from batch file to C# rich text box but I am keep getting failure of doing it.
The procedure works like click button->run batch file secretly->C# gets data lively->display in rich text box
I was successful to run a batch file but it runs in another new CMD causing hanging problem during debugging.
I would like to know whether anyone can wrote me a code to overcome such problem. Hope for the best answer
ProcessStartInfo cmd = new ProcessStartInfo();
Process process = new Process();
cmd.FileName = #"cmd";
cmd.UseShellExecute = false;
cmd.RedirectStandardError = true;
cmd.RedirectStandardInput = true;
cmd.RedirectStandardOutput = true;
cmd.CreateNoWindow = true;
process.EnableRaisingEvents = false;
process.StartInfo = cmd;
process.Start();
process.StandardInput.Write(#"cd C:\Users\%username%\Desktop\Claymore's Dual Ethereum+Decred_Siacoin_Lbry_Pascal AMD+NVIDIA GPU Miner v11.0" + Environment.NewLine);
process.StandardInput.Write(#"EthDcrMiner64.exe -allpools 1 -epool asia1.ethereum.miningpoolhub.com:20535 -ewal AJStudio.AJStudio001 -epsw x -esm 2" + Environment.NewLine);
process.StandardInput.Close();
string result = process.StandardOutput.ReadToEnd();
StringBuilder sb = new StringBuilder();
sb.Append("[result info]" + DateTime.Now + "\r\n");
sb.Append(result);
sb.Append("\r\n");
richTextBox1.Text = sb.ToString();
process.WaitForExit();
process.Close();
To get real-time feedback from Process, use OutputDataReceived and ErrorDataReceived events. Something like this:
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.OutputDataReceived += Process_OutputDataReceived;
process.ErrorDataReceived += Process_ErrorDataReceived;
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
private void Process_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
if (e.Data == null) return;
log("ERROR: " + e.Data);
}
private void Process_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
if (e.Data == null) return;
log(e.Data);
}

Process Wait For Exit not work

I'm using the below code to download from youtube using youtube-dl python script.
string pythonPath = #"C:\Python35\python.exe";
string ydl = #"C:\Y\ydl\youtube-dl";
string tempLocation = Server.MapPath("/ydl/");
string Output = "";
string Error = "";
int numOutputLines = 0;
int numErrorLines = 0;
using (Process process = new Process())
{
process.EnableRaisingEvents = true;
process.StartInfo.ErrorDialog = false;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.FileName = pythonPath;
process.StartInfo.WorkingDirectory = tempLocation;
process.StartInfo.Arguments = ydl + " --output test.mp4 --force-ipv4 -f bestvideo[ext=mp4]+bestaudio[ext=m4a] \"" + Url + "\"";
process.StartInfo.Verb = "runas";
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
StringBuilder output = new StringBuilder();
StringBuilder error = new StringBuilder();
using (AutoResetEvent outputWaitHandle = new AutoResetEvent(false))
using (AutoResetEvent errorWaitHandle = new AutoResetEvent(false))
{
process.OutputDataReceived += (sender, e) =>
{
if (e.Data == null)
{
outputWaitHandle.Set();
}
else
{
numOutputLines++;
this.Context.Response.Write(Environment.NewLine + "[" + numOutputLines.ToString() + "] - " + e.Data);
output.AppendLine("[" + numOutputLines.ToString() + "] - " + e.Data);
}
};
process.ErrorDataReceived += (sender, e) =>
{
if (e.Data == null)
{
errorWaitHandle.Set();
}
else
{
numErrorLines++;
this.Context.Response.Write(Environment.NewLine + "[" + numErrorLines.ToString() + "] - " + e.Data);
error.AppendLine("[" + numErrorLines.ToString() + "] - " + e.Data);
}
};
//process.Exited += (s, a) =>
//{
// process.Close();
//};
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
//process.WaitForExit();
Process[] curProcess = Process.GetProcessesByName("youtube-dl");
Process youtubeProcess = curProcess.FirstOrDefault();
while (!youtubeProcess.HasExited)
{
Thread.Sleep(100);
}
Output = output.ToString();
Error = error.ToString();
process.Close();
}
}
I used the proccess in this way because I want to have the percentage of youtube-dl script for showing in my client side progress bar.
But there are some problems and it's that WaitForExit is not working. I read from other topics that this issue is related to wait in process not working for child process(I mean in my way, the wait for exit works for python not for youtube-dl script)
What should I do?
Since you are interested in a child process maybe you an try to poll on the youtube process by using the method:
Process.GetProcessesByName(string processName);
Something like this:
Process[] curProcess = Process.GetProcessesByName("your youtube process name");
Process youtubeProcess = curProcess.FirstOrDefault(); // Get here the right process instance
while (!youtubeProcess.HasExited)
{
Thread.Sleep(100);
}

cmd command in c#

I want to import a csv file to mongodb by using mongoimport in C#. So I implement this method
public bool importCSV(string filepath, string db, string collectionName){
string result="";
try
{
ProcessStartInfo procStart = new ProcessStartInfo("cmd", "C:/MongoDB/Server/3.0/bin/mongoimport -d " + db + " -c " + collectionName + " --type csv --file " + filepath );
procStart.RedirectStandardOutput = true;
procStart.CreateNoWindow = false;
Process proc = new Process();
proc.StartInfo = procStart;
proc.Start();
result += proc.StandardOutput.ReadToEnd();
}
catch(Exception e){
Console.WriteLine(e.ToString());
}
if (!result.Equals("")){
return true;
}
return false;
}
When I run command itself, I can import file to MongoDB. But by using C#, method returns false.
Can anyone help me to solve this problem?
SOLUTION!!!
public bool importCsv(string filepath, string collectionName){
string result ="";
try
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = #"C:/MongoDB/Server/3.0/bin/mongoimport.exe";
startInfo.Arguments = #" -d test -c " + collectionName + " --type csv --file " + filepath + " --headerline";
Process proc = new Process();
proc.StartInfo = startInfo;
proc.Start();
result += "ddd";
}
catch(Exception e)
{
Console.WriteLine(e.ToString());
}
if (!result.Equals(""))
{
return true;
}
return false;
}
try something like this:
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.OutputDataReceived += new DataReceivedEventHandler((sender, e) =>
{
if (!String.IsNullOrEmpty(e.Data))
{
result+=e.Data;
}
});
process.Start();
// Asynchronously read the standard output of the spawned process.
// This raises OutputDataReceived events for each line of output.
process.BeginOutputReadLine();
process.WaitForExit();
process.Close();

How to display output of iperf cmd prompt in textbox

I am using iperf-2.0.5-2-win32 tool to find network bandwidth. I have written codes in c# which opens the cmd prompt, pass iperf parameters to start server side & client side. iperf-2.0.5-2-win32 exe will not open directly, need to open through cmd prompt only.
At present the output(Transfer rate & Bandwidth) is displaying on cmd prompt itself. I want these output to be displayed in textbox
I have tried StreamReader also. But it takes null, I have also tried OutputDataReceived Event, its also taking null.
Found few codes for ipconfig & ping.but those were not working with iperf codes.
button_click event(),
{
Process Client_proc = new Process();
ProcessStartInfo Client_command = new ProcessStartInfo("cmd.exe");
string ip = txtIP.Text;
Client_command.CreateNoWindow = true;
Client_command.WindowStyle = ProcessWindowStyle.Hidden;
Client_command.WorkingDirectory = #"E:\Iperf\RunEXE_Through_Application\iperf-2.0.5-2-win32";
Client_command.Arguments = "/c START iperf -c " + ip;
Client_proc.StartInfo = Client_command;
Client_command.RedirectStandardOutput = true;
Client_command.UseShellExecute = false;
Client_proc.OutputDataReceived += new DataReceivedEventHandler(Client_proc_OutputDataReceived);
Client_proc.Start();
Client_proc.BeginOutputReadLine();
Client_proc.WaitForExit();
}
void Client_proc_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
if (e.Data != null)
{
string newLine = e.Data.Trim() + Environment.NewLine;
MethodInvoker append = () => txtOutput.Text += newLine;
txtOutput.BeginInvoke(append);
}
}
Plz help me.Earlier responses are appreciated
Thanks
you use this complete code for your disposal
It is not perfect (some problems when using multiple streams )
public void RunProcess(string FileName, string Arguments, bool EventWhenExit )
{
process = new Process();
process.EnableRaisingEvents = true;
process.OutputDataReceived += new DataReceivedEventHandler(OnDataReceivedEvent);
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.LoadUserProfile = false;
process.StartInfo.UseShellExecute = false;
process.StartInfo.FileName = FileName; // Gets or sets the application or document to start.
process.StartInfo.Arguments = Arguments;//Gets or sets the set of command-line arguments to use when starting the application
Thread.Sleep(1000);
if (EventWhenExit)
{
process.EnableRaisingEvents = true;
process.Exited += new EventHandler(myprocess_Exited);/*New line */
}
process.Start();
process.BeginOutputReadLine();
PID = process.Id;
}
private void myprocess_Exited(object sender, EventArgs e)
{
process.Refresh();
Thread.Sleep(2000);
onProcessEnd(this, "ENDOF " + Proc.ToString());
Console.WriteLine("Process exsiting ");
}
private void OnDataReceivedEvent(object sender, DataReceivedEventArgs e)
{
string OutputFromProcess = e.Data;
//fire event to event handler class for further use
onDataOutputFromProcess(this, OutputFromProcess, Proc.ToString());
}
than in your GUI layer you should bind to onDataOutputFromProcess event
there you should have something like
if (screenToPrint.InvokeRequired) //&& this.Visible)
{
try
{
this.Invoke(new Action<AppendToScreenParam>(AppendTextFullConfig), new object[] { append });
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
return;
}
else
{
screenToPrint.SelectionFont = font;
screenToPrint.SelectionColor = append.Color;
//screenToPrint.AppendText(append.Message);
string TextToPrint = string.Format("{0}\n", append.Message);
screenToPrint.AppendText(TextToPrint);
}
}
Maybe it is because iperf process is returning error. Subscribe the ErrorDataReceived event with Client_proc.ErrorDataReceived += Client_proc_ErrorDataReceived; and see the results. If command returns error, you can see the error message as output.
void Client_proc_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
if (e.Data != null)
{
this.txtOutput.BeginInvoke(new MethodInvoker(() => { this.txtOutput.Text = e.Data; }));
}
}

Categories

Resources