Question Showing Console Output On Label Is Not Working

SiamIT

Active member
Joined
Aug 3, 2021
Messages
44
Programming Experience
5-10
Greetings,

i am just trying to show the console output to label text. but it's just not working..

i have tired hours and hours of this and that but none is working.. and now came here for support

here is the code i have tried so far:

C#:
        private static void ExecuteCLI(string[] args, string workingDir, Form form, Label label)
        {
            List<string> lArgs = new List<string>(args);
            lArgs.RemoveRange(0, 3);

            string cliFile = args[2];
            string cliArgs = string.Join(" ", lArgs.ConvertAll(item => item.Contains(" ") == true ? '"' + item + '"' : item));
            //create working directory if not exits
            if(Directory.Exists(workingDir) == true && isIde == true)
            {
                label.Text = "Deleting Working Dir...";
                foreach(string filePath in Directory.GetFiles(workingDir, "*.*", SearchOption.AllDirectories))
                {
                    new FileInfo(filePath).Attributes = FileAttributes.Normal;
                    File.Delete(filePath);
                }
                Directory.Delete(workingDir, true);
                label.Text = "Status Goes Here...";
            }
            if(Directory.Exists(workingDir) == false)
            {
                Directory.CreateDirectory(workingDir);
            }

            Process process = new Process()
            {
                StartInfo = new ProcessStartInfo()
                {
                    FileName = cliFile,
                    Arguments = cliArgs,
                    UseShellExecute = false,
                    WindowStyle = ProcessWindowStyle.Hidden,
                    CreateNoWindow = true,
                    RedirectStandardOutput = true,
                    WorkingDirectory = workingDir,
                },
            };

            process.OutputDataReceived += delegate (object sender, DataReceivedEventArgs e)
            {
                if(e.Data == null)
                {
                    return;
                }
                string cliMSG = e.Data.Trim();
                if(cliMSG.StartsWith("Extracting") == true)
                {
                    System.Diagnostics.Debug.WriteLine(cliMSG); //if no method is used, it's shows the output on debug/output window just fine..
                    
                    //sUpdateAddressBar(form, label, cliMSG); (3rd Method, crash/keeps loading forever without updating)
                    
                    //setLabelText(label, cliMSG);  (2nd Method, crash/keeps loading forever without updating)
                    
                    /* // (1st method though don't crash the app, just don't updte the
                    form.BeginInvoke((Action)(() =>
                    {

                        label.Text = cliMSG;
                    }));
                    */
                }
            };
            process.Start();
            process.BeginOutputReadLine();
            process.WaitForExit();
            process.Dispose();

            form.DialogResult = DialogResult.OK;
        }

        private static void setLabelText(Label label, string text)
        {
            if(label.InvokeRequired)
            {
                label.Invoke((System.Action)(() => setLabelText(label, text)));
            }
            else
            {
                label.Text = text;
            }
        }

        private delegate void sCBUpdateAddresBar(Form form, Label label, string text);
        private static void sUpdateAddressBar(Form form, Label label, string text)
        {
            if(form.InvokeRequired)
            {
                form.Invoke(new sCBUpdateAddresBar(sUpdateAddressBar), form, label, text);
            }
            else
            {
                label.Text = text;
            }
        }

as i have noted in comments no method i have tried does works :(

here is the screen shot of crashed/loading forever: (i red marked the area to identify the cursor)
CLI-Status-Error.png


can any one suggest me what i am doing wrong? here?

best regards
 
My first guess would be that process.WaitForExit(); is blocking the Ui thread so the invokes won't work (you're invoking so that you can update the UI from a non UI thread which is good, but if you block the UI thread anyway it won't be available to draw the UI when the invoke is done)
 

Latest posts

Back
Top Bottom