简体   繁体   English

在C#中输出对Label的命令行响应

[英]Output Command Line response to Label in C#

EDIT : Thanks lunarquaker for the help. 编辑 :感谢lunarquaker的帮助。 Below is the now working code with the setLabel method from the approved answer. 以下是已批准答案中带有setLabel方法的当前工作代码。 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 变量“ sender”和“ e”被更改为“ sender2”和“ e2”,因为这是在按钮内部发生的,该按钮已经在使用“ sender”和“ e”变量

        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 和setLabel方法

    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. 我正在编写一个C#应用程序,该程序处理创建参数文件,然后将该文件和二进制文件刷新到微处理器。 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. 在后端,我实际上是在使用uniflash命令行工具进行刷新,但是我想对用户隐藏任何看起来“计算机化”或“技术化”的内容(因为我认为这会吓到他们)我想在运行时隐藏CMD,并且出于相同的原因,我不想打开并显示控制台,但是我想从CMD中逐行获取响应,然后将该响应写到一个标签上,我的用户将会看到。 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) 我希望该标签在大多数情况下变化太快,以至于用户无法阅读它,但是它会提醒他们任何问题(主要是确保设备已插入并且开关设置为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. 我知道在开始之前,我需要订阅OutputDataRecieved事件并将其设置为我的标签。 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. 我以该线程为例,但是当我尝试将其插入时,它抱怨(sender,e)部分,因为这些不是它可以找到的变量。 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 我也认识到Console.WriteLine(stuff)与设置标签的text属性并不完全相同,而且我不知道如何使用最新消息连续更新该标签

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?) 我将继续阅读OutputDataReceived的文档,以了解是否可以了解我在哪里做错了(除非有人在这里回答),但是我真的很感谢将标签附加到标签上的任何帮助(或者实际上很简单)将输出设置为text属性?)

Maybe I am missing something. 也许我缺少了一些东西。 Does it not work to create a setLabelText method with an InvokeRequired check? 用InvokeRequired检查创建setLabelText方法不起作用吗? First, your subscription to the OutputDataReceived event you associate it with a method that sets the label: 首先,您对OutputDataReceived事件的订阅将其与设置标签的方法相关联:

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;
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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