Ho costruito Form
App che uso per qualche tempo, ora voglio prendere il StandardError
del mio processo così come la sua standartOutput
potete ottenere output di errore di processo utilizzando process.ErrorDataReceived C#
Ho guardato risposte in SO e MSDN e tuttavia e non posso farlo bene
Il mio codice:
public void RunProcess(string FileName, string Arguments,, bool IsPrintOutput = true)
{
process = new Process();
process.ErrorDataReceived += new DataReceivedEventHandler(OnDataReceivedEvent);
if (IsPrintOutput) process.OutputDataReceived += new DataReceivedEventHandler(OnDataReceivedEvent);
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.FileName = FileName;
process.StartInfo.Arguments = Arguments;
if (EventWhenExit)
{
process.EnableRaisingEvents = true;
process.Exited += new EventHandler(myprocess_Exited);
}
process.Start();
process.BeginOutputReadLine();
//run polling on stored logs to print them to screen
PollingService();
}
ho controllare con Iperf e vedo che quando l'eseguo con l'argomento corretto ricevo uscita corretta ma quando basta inviare con qualsiasi argumnet vedo che con cmd ricevo
C:\>iperf.exe
Usage: iperf [-s|-c host] [options]
Try `iperf --help' for more information.
E la mia app ottengo Niente !
cosa mi manca qui? Grazie
È possibile interrompere la lettura qui! Se si desidera visualizzare i dettagli di metodo interno continuerà sotto:
private void OnDataReceivedEvent(object sender, DataReceivedEventArgs e)
{
string ProcessOutput = e.Data;
ProcessLog.Add(e.Data);
}
private void PollingService()
{
var T = new Thread (()=>
{
while (true /* ProcessRunning*/)
{
if (ProcessLogIndex < ProcessLog.Count)
{
lock (this)
{
var tempList = ProcessLog.GetRange(ProcessLogIndex, ProcessLog.Count - ProcessLogIndex);
ProcessLogIndex = ProcessLog.Count;
foreach (var ToSend in tempList)
{
onDataOutputFromProcess(this, ToSend, sProcessNameID.ToString());
}
}
}
Thread.Sleep(400);
}
});
T.IsBackground = true;
T.Start();
}
sei così GIUSTO! :) Non posso credere di averlo dimenticato – LordTitiKaka