简体   繁体   中英

Output Command Line response to Label in C#

EDIT : Thanks lunarquaker for the help. Below is the now working code with the setLabel method from the approved answer. the variables "sender" and "e" were changed to "sender2" and "e2" because this was happening inside of a button which was already using the "sender" and "e" variables

        Process process = new Process();
        string command = @"/K C:\ti\uniflash_3.4\uniflashCLI.bat -config C:\users\david\desktop\twinHM\TTwin9V.usf -setOptions com=3 -operations program";
        process.StartInfo.FileName = "CMD.exe";
        process.StartInfo.Arguments = command;
        process.StartInfo.RedirectStandardOutput = true;
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.CreateNoWindow = true;
        process.OutputDataReceived += (sender2, e2) => setLabelText(lblCMDResponse, e2.Data);
        lblCMDResponse.Visible = true;
        process.Start();
        process.BeginOutputReadLine();

and the setLabel method

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

I'm writing a C# application that handles creating a parameter file and then flashes that file and a binary to a microprocessor. On the back end i'm actually using the the uniflash command line tool to do the flashing, but I want to hide anything that looks "computery" or "techy" from my users (because I think it will scare them) For this reason I want to hide CMD when it is running, and I don't want to open and show a console for the same reason, but I want to get the response (line by line) from CMD and then write that response to a label that my user will see. For the most part I expect this label to change far too quickly for the user to read it, but it will alert them to any issues (the main one being making sure the device is plugged in and the switch is set to FTDI)

I'm having trouble figuring out how to do this.

This is what I'm doing in that section so far:

Process process = new Process();
string command = @"/K C:\ti\uniflash_3.4\uniflashCLI.bat -config C:\users\david\desktop\twinHM\TTwin9V.usf -setOptions com=3 -operations program";
process.StartInfo.FileName = "CMD.exe";
process.StartInfo.Arguments = command;
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.Start();

I know that before the start I need to subscribe to the OutputDataRecieved event and set it to my label somehow. I was using this thread as an example, but when I try to plug it in it complains about the (sender, e) part because those aren't variables that it can find. I also recognize that Console.WriteLine(stuff) isn't exactly the same as just setting the text property of a label and I don't know how I would go about continuously updating that label with the most recent message

I'll keep reading through the documentation for OutputDataReceived to see if I can get an understanding of what im doing wrong there (unless someone answers here) but I would really appreciate any help attaching that to the label (or is it actually as simple as setting the output to the text property?)

Maybe I am missing something. Does it not work to create a setLabelText method with an InvokeRequired check? First, your subscription to the OutputDataReceived event you associate it with a method that sets the label:

process.OutputDataReceived += (sender, e) => setLabelText(myLabel, e.Data);

Then, the method that updates the label:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM