I have a BackgroundWorker
thread that runs a cmd process and writes multiple commands to it. Some of the commands may take a while to complete so I want to show the user the cmd output of the progress.
My code for running the cmd commands looks like this:
private void backgroundWorker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
var startInfo = new ProcessStartInfo("cmd.exe")
{
UseShellExecute = false,
RedirectStandardInput = true,
RedirectStandardOutput = true,
CreateNoWindow = true
};
cmd = new Process { StartInfo = startInfo };
cmd.OutputDataReceived += Cmd_OutputDataReceived;
cmd.Start();
cmd.BeginOutputReadLine();
cmd.StandardInput.WriteLine($"cd {baseFolder}\\External Resources\\img files\\uboot");
string[] commands = CmdCommands.GetUbootFlashCommands();
foreach (var command in commands)
cmd.StandardInput.WriteLine(command);
cmd.StandardInput.WriteLine($"cd {baseFolder}\\External Resources\\img files\\android");
commands = CmdCommands.GetKernelFlashCommands();
foreach (var command in commands)
cmd.StandardInput.WriteLine(command);
cmd.StandardInput.WriteLine("exit");
cmd.WaitForExit();
}
private void Cmd_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
Invoke(new Action(() =>
{
txtCmd.Text += e.Data + Environment.NewLine;
}));
}
But the cmd output loos nothing the real output as shown in a regular cmd window. Here is how my output looks like:
Microsoft Windows [Version 10.0.14393]
(c) 2016 Microsoft Corporation. All rights reserved.
C:\>cd C:\img files\uboot
C:\img files\uboot>fastboot flash XXX.bin
C:\img files\uboot>fastboot flash XXX.bin
C:\img files\uboot>cd C:\img files\android
C:\img files\android>fastboot flash kernel
C:\img files\android>fastboot flash system XXX.img
C:\img files\android>fastboot flash userdata XXX.img
C:\img files\android>fastboot flash cache XXX.img
C:\img files\android>exit
And here is how output looks like when I open a cmd window and type in the commands:
Microsoft Windows [Version 10.0.14393]
(c) 2016 Microsoft Corporation. All rights reserved.
C:\WINDOWS\system32>cd C:\img files\uboot
C:\img files\uboot>fastboot flash bl2 bl2.bin
target didn't report max-download-size
sending 'bl2' (14 KB)...
OKAY [ 0.006s]
writing 'bl2'...
OKAY [ 0.042s]
finished. total time: 0.052s
C:\img files\uboot>fastboot flash bootloader u-boot.bin
target didn't report max-download-size
sending 'bootloader' (275 KB)...
OKAY [ 0.049s]
writing 'bootloader'...
OKAY [ 0.046s]
finished. total time: 0.098s
C:\img files\uboot>cd C:\img files\android
C:\img files\android>fastboot flash kernel zImage-dtb
target didn't report max-download-size
sending 'kernel' (5099 KB)...
OKAY [ 0.839s]
writing 'kernel'...
OKAY [ 0.145s]
finished. total time: 0.988s
C:\img files\android>fastboot flash system system.img
target didn't report max-download-size
sending 'system' (426874 KB)...
OKAY [ 70.327s]
writing 'system'...
OKAY [ 30.963s]
finished. total time: 101.295s
C:\img files\android>fastboot flash userdata userdata.img
target didn't report max-download-size
sending 'userdata' (35680 KB)...
OKAY [ 5.895s]
writing 'userdata'...
OKAY [ 2.301s]
finished. total time: 8.200s
C:\img files\android>fastboot flash cache cache.img
target didn't report max-download-size
sending 'cache' (6248 KB)...
OKAY [ 1.036s]
writing 'cache'...
OKAY [ 0.380s]
finished. total time: 1.422s
C:\img files\android>
How do I get this output in my my C# code???