Answer the question
In order to leave comments, you need to log in
How to get tracked and persistent as a ProcessStartInfo thread in C#?
In general, I launch it like this, setting the settings and parameters:
ProcessStartInfo psi = new ProcessStartInfo();
// Тут найстройки, параметры и т.п.
using (var process = Process.Start(psi))
{
errors = process.StandardError.ReadToEnd();
output = process.StandardOutput.ReadToEnd();
}
Answer the question
In order to leave comments, you need to log in
Created a class to track the future process
var process = new Process
{
StartInfo = psi
};
process.OutputDataReceived += (sender, args) => Display(args.Data);
process.ErrorDataReceived += (sender, args) => Display(args.Data);
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
var exited = process.WaitForExit(0);
No way. ProcessStartInfo worked and died. You yourself said you need a flow, so create it.
Better wrap everything in a class where you will pull your script once every 200 ms.
public class ProcInfo{
public DateTime Start{get; private set;}
public DateTime Stop{get; private set;}
public string Errors{get; private set;}
public string Output{get; private set;}
// .... что то еще
}
var procInfo = new List<ProcInfo>();
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
var worker = new Worker();
worker.Start();
Thread.Sleep(1000); // тут можно поставить Console.ReadLine(); и ждать вашего ввода, в общем на ваше усмотрение.
Console.ReadLine();
worker.EmergencyExit();
}
}
public class Worker
{
private const string PythonPath = @"C:/Python/Python38-32/python.exe";
private const string PyScript = @"Load.py";
private const int Interval = 200;
private Thread _thread = null;
private readonly List<ProcInfo> _infos = new List<ProcInfo>();
private void Run()
{
var processInfo = new ProcInfo { Start = DateTime.Now };
var psi = new ProcessStartInfo
{
FileName = PythonPath,
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
Arguments = $"\"{PyScript}\" "/*// опустил для ясности все остальные аргументы*/
};
try
{
using var process = Process.Start(psi);
processInfo.Error = process.StandardError.ReadToEnd();
processInfo.Output = process.StandardOutput.ReadToEnd();
processInfo.Stop = DateTime.Now;
_infos.Add(processInfo);
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
Thread.Sleep(Interval);// эмулируем работу на самом деле у меня ничего не работает
processInfo.Error = e.Message;
processInfo.Stop = DateTime.Now;
_infos.Add(processInfo);
}
var delay = processInfo.Stop - processInfo.Start;
if (delay.TotalMilliseconds < Interval) Thread.Sleep(Interval - (int)delay.TotalMilliseconds); // скрипт отработал быстрее чем нужно ждем
Run(); // перезапускаем себя
}
public void Start()
{
_thread = new Thread(new ThreadStart(Run));
_thread.Start();
}
public void EmergencyExit()
{
_thread?.Abort();
}
}
public class ProcInfo
{
public DateTime Start { get; set; }
public DateTime Stop { get; set; }
public string Output { get; set; }
public string Error { get; set; }
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question