Click here to Skip to main content
15,891,770 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a console application that is counting a percentage up, and it takes some time to finish.
I can call and read it's output inside a winapplication, but inside a single global variable, that gather everything while the console app is running, and AfteR all is finished, myvar will finally display its content. Practical, but Not Nice !
I want to see the console output updating live in my win app !
Thank you !

What I have tried:

C#
       void ComandExecute()
        {
            //if (label1.InvokeRequired)  //I tried this but is getting skipped
            {

                string nl = "\r\n";
                string vvvh = "";

                Main.Text = "";
                var processInfo = new ProcessStartInfo("console_app.exe", "output> " + str1);
                processInfo.CreateNoWindow = true;
                processInfo.UseShellExecute = false;
                processInfo.RedirectStandardError = true;
                processInfo.RedirectStandardOutput = true;

                var process = Process.Start(processInfo);

//I tried here to obtain the output into my label but i get error01
//error01 says: 
//" Cross-thread operation not valid: Control 'label1' accessed from a thread other than the //thread it was created on. "

                process.OutputDataReceived += (object sender, DataReceivedEventArgs e) =>
                    {
                        label1.Text += e.Data + nl; label1.Refresh(); //<< error at this line
                    };
                process.BeginOutputReadLine();


                process.ErrorDataReceived += (object sender, DataReceivedEventArgs e) =>
                vvvh += "error>>" + e.Data + nl;
                label1.Text += vvvh; label1.Refresh();
                process.BeginErrorReadLine();


                process.WaitForExit();
                vvvh += "ExitCode: " + process.ExitCode + nl;
                process.Close();

                Main.Text = vvvh;
                label1.Text = Main.Text;
            }
        }
Posted
Updated 26-Mar-20 3:24am

First off, add your handlers before you call Start on the process - or otherwise, you will lose any data the app generates before you add the handlers.

Second, the DataReceived events are always handled on a separate thread unless you set the Process.SynchronizingObject Property (System.Diagnostics) | Microsoft Docs[^] which synchronises data onto the UI thread for you.

Be aware that the OutputDataReceived event only works in complete lines.
 
Share this answer
 
Comments
_Q12_ 26-Mar-20 9:19am    
I dont know what handler is.
The program is working fine if i leave myvar to collect the data instead of my little intervention with the label on that line. But it does it all at once and then show the result.
Dave Kreskowiak 26-Mar-20 9:54am    
event handlers.... those lines that mention "+= (object sender, ...)" are setting up event handlers.

You didn't really write this code, did you? Copy and paste?
_Q12_ 26-Mar-20 9:56am    
yes, i copy and paste most of it.
I'm getting some results, but not the ones i really want.
Thank you for all your kind help and understanding so far !
_Q12_ 26-Mar-20 10:53am    
@ Dave Kreskowiak, Im used to call them simply "events". Im not used to call them "handlers" but now i get it they have 3 names for the same thing: "event", "handler" and "event handler". I was aware of them named like that but really using only 1 naming. Thanks for clarification.
OriginalGriff 26-Mar-20 10:57am    
The "Event" is the thing you handle; The "handler" or "event Handler" is the code that runs when the Event occurs.
TextBox.TextChanged is an Event: your code that checks what the user typed is the Handler.
If you want the information in a Windows application then use a BackgroundWorker Class (System.ComponentModel) | Microsoft Docs[^] to do the calculations. That way you can get the value updated as it is being calculated.
 
Share this answer
 
Comments
_Q12_ 4-Apr-20 19:12pm    
I give it another try today and the code from your link did it's job wonderfully.
Thank you very much for help and understanding.
Richard MacCutchan 5-Apr-20 2:45am    
You are welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900