Obtenir une sortie de ligne de commande de manière dynamic

J’exécute un programme à l’aide de la ligne de commande en c #. Ce programme génère des journaux, mais il doit être affiché à chaque modification. J’ai écrit le code suivant, mais il affiche tous les journaux une fois le processus tué et pendant le temps d’exécution de mon programme ne répond plus. Comment puis-je le réparer?

Cordialement

ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", "/c " + "C:\\server.py"); Process proc = new Process(); procStartInfo.WindowStyle = ProcessWindowStyle.Hidden; procStartInfo.UseShellExecute = false; procStartInfo.RedirectStandardOutput = true; //procStartInfo.CreateNoWindow = true; proc.StartInfo = procStartInfo; proc.Start(); ssortingng output = proc.StandardOutput.ReadToEnd(); proc.WaitForExit(300); LogstextBox.Text = output; 

Édité: eh bien, j’ai essayé d’utiliser OutputDataReceived mais il ne montre aucun résultat, voici le code modifié:

 { //processCaller.FileName = @"ping"; //processCaller.Arguments = "4.2.2.4 -t"; this is working processCaller.FileName = @"cmd.exe"; processCaller.Arguments = "/cc:\\server.py"; //this is not working processCaller.StdErrReceived += new DataReceivedHandler(writeStreamInfo); processCaller.StdOutReceived += new DataReceivedHandler(writeStreamInfo); processCaller.Completed += new EventHandler(processCompletedOrCanceled); processCaller.Cancelled += new EventHandler(processCompletedOrCanceled); this.richTextBox1.Text = "Server Started.." + Environment.NewLine; processCaller.Start(); } private void writeStreamInfo(object sender, DataReceivedEventArgs e) { this.richTextBox1.AppendText(e.Text + Environment.NewLine); } 

C’est le problème:

 ssortingng output = proc.StandardOutput.ReadToEnd(); 

Vous n’atteindrez pas la “fin” de la sortie standard tant que le processus n’est pas terminé.

Vous devriez lire une ligne à la fois ou simplement vous abonner à l’événement OutputDataReceived (et respecter les autres exigences documentées pour cet événement).

EDIT: Voici un exemple de code qui fonctionne pour moi:

 using System; using System.Diagnostics; using System.Threading; class Program { public static void Main() { ProcessStartInfo startInfo = new ProcessStartInfo("cmd", "/c " + "type Test.cs") { WindowStyle = ProcessWindowStyle.Hidden, UseShellExecute = false, RedirectStandardOutput = true, CreateNoWindow = true }; Process process = Process.Start(startInfo); process.OutputDataReceived += (sender, e) => Console.WriteLine(e.Data); process.BeginOutputReadLine(); process.WaitForExit(); // We may not have received all the events yet! Thread.Sleep(5000); } } 

Notez que dans votre exemple de code, vous accédez à l’interface utilisateur quel que soit le thread OutputDataReceived gestionnaire OutputDataReceived – cela me semble une mauvaise idée.

Vous pouvez utiliser la méthode Process.BeginOutputReadLine . Le lien montre un exemple de travail complet en C # qui utilise l’ OutputDataReceived event . Cet exemple de code devrait faire ce que vous voulez.