async interface for python script - c#

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();

Related

Why does redirected standard output appear after process has finished?

I am working with Visual Studio 2015 and .NET framework 4.7.2. I have set up a simple test program that executes an external program in C#. The program is a Python script that simply prints some string to stdout every 0.5 seconds. I want to read the stdout of this sub process in my C# application.
The program basically works, but I get the output of the Python script only shortly before the sub process exits. What do I need to change in order to get a more responsive behavior, i.e. getting the output every 0.5 second as soon as the Python script writes it to stdout?
Here's my C# code:
public class Program {
private Process process;
public static void Main(string[] args) {
new Program().init();
}
private void init() {
startPythonProcess();
process.WaitForExit();
Console.ReadLine();
}
private void startPythonProcess() {
if(process==null) {
try {
Console.WriteLine("Starting Python process ...");
string filepath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase).Substring(6);
process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
startInfo.WorkingDirectory = filepath;
startInfo.FileName = "python.exe";
//startInfo.WindowStyle = ProcessWindowStyle.Normal;
startInfo.RedirectStandardInput = false;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.Arguments = string.Format("{0}", Path.Combine(filepath, "test.py"));
process.StartInfo = startInfo;
process.OutputDataReceived += OutputDataReceivedEventHandler;
process.ErrorDataReceived += ErrorDataReceivedEventHandler;
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
} catch(Exception ex) {
Console.WriteLine("Could not start Python process: " + ex.Message);
}
}
}
public void OutputDataReceivedEventHandler(object sender, DataReceivedEventArgs args) {
Console.WriteLine("[PYTHON] INFO: {0}", args.Data);
}
public void ErrorDataReceivedEventHandler(object sender, DataReceivedEventArgs args) {
Console.WriteLine("[PYTHON] ERROR: {0}", args.Data);
}
}
Here's my Python script:
import time
import sys
import logging
logging.basicConfig(level=logging.ERROR)
if __name__ == '__main__':
count = 0
while True:
print('PYTHON: {}'.format(count))
time.sleep(0.5)
count+=1
if count>=25:
break
UPDATE: I uploaded the mini project here.
The print function takes a flush argument which controls whether buffered output is flushed.
The default value of flush is False, meaning flushing is controlled by whatever file print is writing to (for example, sys.stdout).
Set flush to True to force immediate printing.
print('PYTHON: {}'.format(count), flush=True)

C# FFMPEG Process and Multiple files

I am working on a C# Form tool that will help me convert all of my phone and DSLR video to HEVC, currently, i have a program that uploads the photos and videos to different directories in my home server each time i connect to the WiFi. Once a month or so, i manually convert all the videos, but thought I would automate the process.. I have the Form working perfectly for processing 1 file. but get into trouble when processing a Directory (with possible sub-directories) all at once..
Sorry, this is long, just want to be thorough. here is the button calls
private void processFile_Click(object sender, EventArgs e)
{
OpenFileDialog file = new OpenFileDialog();
file.InitialDirectory = baseMediaDirectory;
if (file.ShowDialog() == DialogResult.OK)
{
ProcessSinlgeFile(file.FileName);
}
}
(above)for one file and (below) for a directory
private void processDirectory_Click(object sender, EventArgs e)
{
FolderBrowserDialog file = new FolderBrowserDialog();
file.SelectedPath = baseMediaDirectory;
if(file.ShowDialog() == DialogResult.OK)
{
ProcessDirectoryOfFiles(file.SelectedPath);
}
}
private void ProcessDirectoryOfFiles(string selectedPath)
{
List<string> listOfFiles = GetAllFiles(selectedPath);
foreach (string s in listOfFiles)
{
ProcessSinlgeFile(s);
}
}
both ultimately call this method, to do some checks and setup
private void ProcessSinlgeFile(string fileName)
{
if (IsAcceptableMediaFile(fileName))
{
outputWindow.AppendText("File to Process: " + fileName);
processMediaFile =
new MediaFileWrapper(this.outputWindow, new MediaFile(fileName), new NReco.VideoInfo.FFProbe());
if (processMediaFile.OkToProcess)
{
int initialCRFValue = 15;
//ultrafast superfast veryfast faster fast medium slow slower veryslow placebo
string intialSpeed = "veryfast";
try {
ConvertToMPEG(processMediaFile.getFFMPEGCommand(initialCRFValue, intialSpeed), processMediaFile);
}
catch
{
// at somepoint, we'll catch a bad file size (or compression)
// then change the CRF value and/or compression speed
}
}
}
}
ultimately I get to this Method and run into trouble.
private async void ConvertToMPEG(string arguments, MediaFileWrapper processMediaFile)
{
startTime = DateTime.Now;
watch = new Stopwatch();
watch.Start();
progressBar1.Minimum = 0;
progressBar1.Maximum = processMediaFile.GetTotalMilliseconds();
// Start the child process.
p = new Process();
//Setup filename and arguments
outputWindow.AppendText("ffmpeg " + arguments);
p.StartInfo.Arguments = arguments;
p.StartInfo.FileName = "ffmpeg.exe";
p.StartInfo.UseShellExecute = false;
// Redirect the output stream of the child process.
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardInput = true;
// capture the date for stdout and std error
// note FFMPEG uses Stderr exclusively
p.ErrorDataReceived += new DataReceivedEventHandler(ErrorDataReceived);
p.OutputDataReceived += new DataReceivedEventHandler(OutputDataReceived);
// Hide Console Window
p.StartInfo.CreateNoWindow = true;
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.Start();
p.BeginErrorReadLine();
p.BeginOutputReadLine();
await p.WaitForExitAsync();
}
and WaitForExitAsync is in another class because in can not be in here with a Form
public static Task WaitForExitAsync(this Process process,
CancellationToken cancellationToken = default(CancellationToken))
{
var tcs = new TaskCompletionSource<object>();
process.EnableRaisingEvents = true;
process.Exited += (sender, args) => tcs.TrySetResult(null);
if (cancellationToken != default(CancellationToken))
cancellationToken.Register(tcs.SetCanceled);
return tcs.Task;
}
however, single files work fine, when I call a directory through, it continuously starts processes for each file, trying to run them all at the same time. You can see I tried implementing this
process.WaitForExit() asynchronously
with no luck.

Running string as a Batch file in c#

I'm writing an application that creates a batch file and then run :
I know I can create a Batch file and run it
no problem with that .
What I want to do is :
once I have created the string that makes the file , Is there is any way to execute the string as a Batch file ?
something like
string BatchFile = "echo \"bla bla\" \n iperf -c 123 ... ... .. "
Diagnostics.Process.Start(BatchFile);
You can run CMD.EXE with /c as an executable and have the rest as arguments :
Process.Start("cmd.exe", "/c echo \"bla bla\" \n iperf -c 123 ... ... .. ");
for me, I am using this code:
Process process;
private void button1_Click(object sender, EventArgs e)
{
process = new Process();
process.StartInfo.UseShellExecute = false;
process.StartInfo.FileName = "cmd";
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardInput = true;
process.Start();
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += new DoWorkEventHandler(worker_DoWork);
worker.RunWorkerAsync();
process.StandardInput.WriteLine("cd d:/tempo" );
process.StandardInput.WriteLine("dir");
}
void worker_DoWork(object sender, DoWorkEventArgs e)
{
string line;
while (!process.StandardOutput.EndOfStream)
{
line = process.StandardOutput.ReadLine();
if (!string.IsNullOrEmpty(line))
{
SetText(line);
}
}
}
delegate void SetTextCallback(string text);
private void SetText(string text)
{
if (this.textBox1.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { text });
}
else
{
this.textBox1.Text += text + Environment.NewLine;
}
}
private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
{
process.StandardInput.WriteLine("exit");
process.Close();
}
You may create your Batch "file" as a long string with lines terminated in \n, exactly as you shown in your example, and then execute that string (I called it a "NotBatch-text") executing cmd.exe and redirecting such string into its Stdin standard handle. This way, your "NotBatch-text" may use a large number of Batch features, like expansion of %variables%, IF and FOR commands nested at any level, and many more. You may also use delayed !variable! expansion if you execute cmd.exe with /V:ON switch. Really, the only things that don't works in the NotBatch-text are: parameters and SHIFT command, and GOTO/CALL :label commands; further details at this post.
If you want to execute a more advanced "NotBatch-text" string, you may even simulate GOTO and CALL :label commands with the aid of a third party program, as described at this post.

ASP.NET/C# display output in runtime

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.

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?

Categories