ASP.NET/C# display output in runtime - c#

I'm trying to start a process ("cmd.exe") and then launching another programs (from cmd.exe) and being able to get the output or send inputs in a textbox.
So i created a new Thread to not freeze the UI and then read the standard output and display it in the textbox.
But it seems that as soon as the process start, the link between my UI and the process is broken.
Here is my code :
public partial class exec2 : System.Web.UI.Page
{
public delegate void Worker();
private static Thread worker;
protected void Page_Load(object sender, EventArgs e)
{
}
public void setTextBox(string s)
{
TextBox1.Text = TextBox1.Text + s;
}
protected void RunEXE()
{
System.Diagnostics.Process proc = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo();
psi.FileName = "cmd.exe";
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
psi.RedirectStandardInput = true;
psi.RedirectStandardError = true;
psi.CreateNoWindow = true;
proc.StartInfo = psi;
setTextBox("Setting the process\n");
// Start the process
proc.Start();
setTextBox("Process started\n");
// Attach the output for reading
System.IO.StreamReader sOut = proc.StandardOutput;
// Attach the in for writing
System.IO.StreamWriter sIn = proc.StandardInput;
// Exit CMD.EXE
sIn.WriteLine("EXIT");
// Close the process
proc.Close();
setTextBox("Process closed");
string results = "";
while (!sOut.EndOfStream)
{
results = results + sOut.ReadLine().Trim() + "\n";
setTextBox(results.Replace(System.Environment.NewLine, "\n"));
}
// Close the io Streams;
sIn.Close();
sOut.Close();
}
protected void Button1_Click(object sender, EventArgs e)
{
Init(Work);
}
protected void Button2_Click(object sender, EventArgs e)
{
setTextBox("TEST\n");
}
public static void Init(Worker work)
{
worker = new Thread(new ThreadStart(work));
worker.Start();
}
public void Work()
{
RunEXE();
}
}
But only "setting the process" is displayed.
I think there is something I don't understand in the UI / process managment.

You are starting a worker thread and a process on the server; the UI render pipe doesn't sit there and wait for them - why would it? (your code just does new Thread(...).Start()). Frankly, you're lucky that you even see "Setting the process" - I would not expect that to keep working in the general case. An http response is disconnected; you won't see updates as other things happen. If you want to update a UI that has been sent to the client, you will need something like polling (ajax) or web-sockets.

Related

Sending DOS commands using Process - missing data on OutputDataReceived callback

I have been looking for a solution before posting but I gave up!
I just want to interactively send DOS command using Standard input. It works well but I always don't get the last line (the prompt line) on the OutputDataReceived callback.
Any ideas?
Process p = null;
private void Start_Click(object sender, EventArgs e)
{
p = new Process();
p.StartInfo = new ProcessStartInfo();
p.EnableRaisingEvents = true;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.FileName = "cmd.exe";
p.ErrorDataReceived += ErrorDataReceived;
p.OutputDataReceived += OutputDataReceived;
p.Start();
p.BeginErrorReadLine();
p.BeginOutputReadLine();
p.StandardInput.WriteLine("dir");
}
private void OutputDataReceived(object sender, DataReceivedEventArgs e)
{
Console.WriteLine(e.Data + "\n");
}
private void WriteToStandardInput_Click(object sender, EventArgs e)
{
p.StandardInput.WriteLine(txt_command.Text); //can be "dir" or "cd temp"
}
Adding p.StandardInput.Close() solves the problem, reason is when you close the input stream it actually terminates the process (which you start using 'p.start'). So as I said, you need start separate process for each command.
~Nilesh
OK, I have found a solution... I have created 2 tasks as shown below which are reading constantly from the output and error stream and print it to a rich text box. The trick was not to use BeginErrorReadLine and BeginOutputReadLine.
I hope I was able to help others...
public partial class Form1 : Form
{
private Process p = new Process();
private SynchronizationContext context;
public Form1()
{
InitializeComponent();
context = SynchronizationContext.Current;
}
private void Start_Click (object sender, EventArgs e)
{
p.StartInfo = new ProcessStartInfo();
p.EnableRaisingEvents = true;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.Start();
Task.Run(() => ReadFromStreamLoop(p.StandardOutput));
Task.Run(() => ReadFromStreamLoop(p.StandardError));
}
private void ReadFromStreamLoop (StreamReader s)
{
int count = 0;
char[] buffer = new char[1024];
do
{
StringBuilder builder = new StringBuilder();
count = s.Read(buffer, 0, 1024);
builder.Append(buffer, 0, count);
context.Post(new SendOrPostCallback(delegate (object state)
{
richTextBox1.AppendText(builder.ToString());
}), null);
} while (count > 0);
}
private void WriteToStandardInput_Click (object sender, EventArgs e)
{
p.StandardInput.WriteLine(txt_command.Text); //can be "dir" or "cd temp"
}
}
If you are looking for event end of command execution, then every time spawn a new process. At the end of processing you can show the prompt on main process. Manage your environment variable through invoking(main) process.
See the example on - https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.process.outputdatareceived?view=netframework-4.7.2

async interface for python script

I have built a winform interface for my python program, my python program is a real time voice assistant, what I need is the interface should response instantly when the python gives outputs. I need to display standard output to the interface instantly. the below program is what I made.
in this code, the interface is not responding properly. python program executes in background continuously and not responding to the voice. i need a program that execute my python program and display the standard output to the winform interface.
namespace #interface
{
public partial class Form1 : Form
{
public static string text;
public Form1()
{
InitializeComponent();
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private async void start_button_Click(object sender, EventArgs e)
{
string line;
int counter=0;
msg.Text = "Hey, Tell me something!";
Task task = new Task(Execute);
task.Start();
}
public void Execute()
{
// full path of python interpreter
string python = #"C:/Users/Jayasooryan/AppData/Local/Programs/Python/Python36-32/python.exe";
// python app to call
string myPythonApp = #"C:/Users/Jayasooryan/AppData/Local/Programs/Python/Python36-32/Avira.py";
// Create new process start info
ProcessStartInfo myProcessStartInfo = new ProcessStartInfo(python);
// make sure we can read the output from stdout
myProcessStartInfo.UseShellExecute = false;
myProcessStartInfo.RedirectStandardOutput = true;
myProcessStartInfo.CreateNoWindow = true;
// start python app with 3 arguments
// 1st arguments is pointer to itself,
// 2nd and 3rd are actual arguments we want to send
myProcessStartInfo.Arguments = myPythonApp;
Process myProcess = new Process();
// assign start information to the process
myProcess.StartInfo = myProcessStartInfo;
// start the process
myProcess.Start();
// Read the standard output of the app we called.
// in order to avoid deadlock we will read output first
// and then wait for process terminate:
StreamReader myStreamReader = myProcess.StandardOutput;
string myString = myStreamReader.ReadLine();
text = myString;
//Console.WriteLine(myString);
/*if you need to read multiple lines, you might use:
string myString = myStreamReader.ReadToEnd() */
// wait exit signal from the app we called and then close it.
myProcess.WaitForExit();
myProcess.Close();
// write the output we got from python app
//Console.ReadLine();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
First: you should not try to read a single line from the python process, but rather use the OutputDataReceived event to be triggered when the process writes new data.
Second: since the output is buffered, you probably want to flush it after writing in your python process.
So here's a simple python script that keeps writing to the standard output (note how stdout.flush is called):
import random
import time
import sys
while True:
rand = random.randint(1, 10)
time.sleep(1)
print rand
sys.stdout.flush()
if rand in (9, 10):
break
And here's a simple Form that reads the output of that very script:
var f = new Form();
var t = new TextBox();
t.Dock = DockStyle.Fill;
t.Multiline = true;
f.Controls.Add(t);
f.Load += (s, e) => {
Process process = new Process();
process.StartInfo.FileName = "python";
process.StartInfo.Arguments = #"d:\script.py";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.OutputDataReceived += (s2, e2) => {
t.Text += e2.Data + Environment.NewLine;
};
process.Start();
process.BeginOutputReadLine();
};
f.ShowDialog();

Stopping a continuous Event in C# with (f.ex.) a button

This is probably simple, and the question might not be very good, but I'm looking for the best or most efficient way to accomplish this:
A button click starts an Event, which then runs a method that continiously pings an IP address. The ping output is displayed in a text box.
A click on the same button stops the ping task.
Here's the (I think) relevant code:
The method run by the Event connected to the Ping button:
private void pingClicked (object sender, EventArgs e) {
pinger();
}
The pinger() method:
private void pinger() {
string command = "/c ping " + ipadrtextbox.Text;
if (contchkbox.Checked) {
command += " -t";
}
ProcessStartInfo procStartInfo = new ProcessStartInfo("CMD", command);
Process proc = new Process();
proc.StartInfo = procStartInfo;
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
proc.Start();
procStartInfo.CreateNoWindow = true;
procStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
proc.OutputDataReceived += new
DataReceivedEventHandler(proc_OutputDataReceived);
proc.Start();
proc.BeginOutputReadLine();
procStartInfo.CreateNoWindow = true;
procStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
}
void proc_OutputDataReceived(object sender, DataReceivedEventArgs e) {
if (e.Data != null) {
string newLine = e.Data.Trim() + Environment.NewLine;
MethodInvoker append = () => pingoutput.AppendText(newLine);
pingoutput.BeginInvoke(append);
}
}
A while-loop in the pinger method results in a complaint that "An async read operation has already been started on the stream.", so that's apparently not the way to go.
Also, I haven't found a way for the method to listen for a buttonpress elsewhere in the application, and then stop the task with roc.CancelOutputRead(). But I expect that's the way the task should be stopped?
Keep the running process as a private member of your class then:
private void pingClicked (object sender, EventArgs e) {
if( process != null && !process.HasExited )
{
process.CancelOutputRead()
process.Kill();
process=null;
} else {
pinger();
}
}

Redirecting cmd Output to textBox

I'm Re-Creating the "command prompt" into a Windows Form.
The application is not working properly; and i can't figure exactly why.
When the form Loads it is suposed to run the cmd.exe (Load the cmd info into "TextBox_Receive"), but it doesnt; and also after writing any command in the "textBox_send" (that sends input); it will only show input after pressing "Enter" key 2 or 3 times.
Any idea what i am missing here?
public partial class Form1 : Form
{
// Global Variables:
private static StringBuilder cmdOutput = null;
Process p;
StreamWriter SW;
public Form1()
{
InitializeComponent();
textBox1.Text = Directory.GetCurrentDirectory();
// TextBox1 Gets the Current Directory
}
private void Form1_Load(object sender, EventArgs e)
{
checkBox1.Checked = true;
// This checkBox activates / deactivates writing commands into the "textBox_Send"
cmdOutput = new StringBuilder("");
p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.EnableRaisingEvents = true;
p.OutputDataReceived += new DataReceivedEventHandler(SortOutputHandler);
p.Start();
SW = p.StandardInput;
p.BeginOutputReadLine();
p.BeginErrorReadLine();
}
private static void SortOutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
// I dont actually understand this part of the code; as this is a "copy" of a snippet i found somewhere. Although it fixed one of my issues to redirect.
{
if (!String.IsNullOrEmpty(outLine.Data))
{
cmdOutput.Append(Environment.NewLine + outLine.Data);
}
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
// Send "Enter Key" - Send Command
if (e.KeyChar == 13)
{
SW.WriteLine(txtbox_send.Text);
txtbox_receive.Text = cmdOutput.ToString();
txtbox_send.Clear();
}
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
// Enable / Disable Sending Commands
if (checkBox1.Checked)
txtbox_send.Enabled = true;
else
txtbox_send.Enabled = false;
}
}
}
You might also try capturing the error data.
To do this:
after your line
p.OutputDataReceived += new DataReceivedEventHandler(SortOutputHandler);
enter this line
p.ErrorDataReceived += new DataReceivedEventHandler(SortOutputHandler);
It might also be a problem with cmd.exe.
I think your issue is the use of OutputDataReceived. In the documentation:
The event is enabled during asynchronous read operations on
StandardOutput. To start asynchronous read operations, you must
redirect the StandardOutput stream of a Process, add your event
handler to the OutputDataReceived event, and call BeginOutputReadLine.
Thereafter, the OutputDataReceived event signals each time the process
writes a line to the redirected StandardOutput stream, until the
process exits or calls CancelOutputRead.
See the example code on that page for more details.
However - I'm not sure you need to go that route. Have you tried just reading directly from the StandardOutput stream?

Print standard output of Process in event handler

I have a console application which I am running as a process from my C# program.
I have made an event handler to be called when this process terminates.
How do I print the Standard output of this process inside the event handler.
Basically, how do I access the properties of a process inside the event handler ?
My code looks like below.
public void myFunc()
{
.
.
Process p = new Process();
p.StartInfo.FileName = "myProgram.exe";
p.StartInfo.RedirectStandardOutput = true;
p.EnableRaisingEvents = true;
p.Exited += new EventHandler(myProcess_Exited);
p.Start();
.
.
}
private void myProcess_Exited(object sender, System.EventArgs e)
{
Console.WriteLine("log: {0}", <what should be here?>);
}
I do not want to make the process object p as a field of the class.
Also, what is the use of System.EventArgs e field ? How can this be used ?
In your event handler
object sender
is the Process object (that is a pretty common pattern by the way throughout the .NET Framework)
Process originalProcess = sender as Process;
Console.WriteLine("log: {0}", originalProcess.StandardOutput.ReadToEnd());
Note also that you have to set:
p.StartInfo.UseShellExecute = false;
to use IO redirection in your Process.
Use like this:
private void myProcess_Exited(object sender, System.EventArgs e)
{
Process pro = sender as Process;
string output = pro.StandardOutput.ReadToEnd()
Console.WriteLine("log: {0}", output);
}
Standart output is nothing else then StreamReader.
One option would be to capture it in a closure:
public void myFunc()
{
Process p = new Process();
p.StartInfo.FileName = "myProgram.exe";
p.StartInfo.RedirectStandardOutput = true;
p.EnableRaisingEvents = true;
p.Exited += new EventHandler((sender, args) => processExited(p));
p.Start();
}
private void processExited(Process p)
{
Console.WriteLine(p.ExitTime);
}

Categories