-3

I am currently struggling with bringing existing data in batch file to C# window form

The whole objective is getting a result lively from batch file to C# rich text box but I am keep getting failure of doing it.

The procedure works like click button->run batch file secretly->C# gets data lively->display in rich text box

I was successful to run a batch file but it runs in another new CMD causing hanging problem during debugging.

I would like to know whether anyone can wrote me a code to overcome such problem. Hope for the best answer

        ProcessStartInfo cmd = new ProcessStartInfo();
        Process process = new Process();
        cmd.FileName = @"cmd";

        cmd.UseShellExecute = false;
        cmd.RedirectStandardError = true;
        cmd.RedirectStandardInput = true;
        cmd.RedirectStandardOutput = true;
        cmd.CreateNoWindow = true;

        process.EnableRaisingEvents = false;
        process.StartInfo = cmd;
        process.Start();
        process.StandardInput.Write(@"cd C:\Users\%username%\Desktop\Claymore's Dual Ethereum+Decred_Siacoin_Lbry_Pascal AMD+NVIDIA GPU Miner v11.0" + Environment.NewLine);
        process.StandardInput.Write(@"EthDcrMiner64.exe -allpools 1 -epool asia1.ethereum.miningpoolhub.com:20535 -ewal AJStudio.AJStudio001 -epsw x -esm 2" + Environment.NewLine);
        process.StandardInput.Close();

        string result = process.StandardOutput.ReadToEnd();
        StringBuilder sb = new StringBuilder();
        sb.Append("[result info]" + DateTime.Now + "\r\n");
        sb.Append(result);
        sb.Append("\r\n");

        richTextBox1.Text = sb.ToString();
        process.WaitForExit();
        process.Close();
AJ Studio
  • 21
  • 2

1 Answers1

1

To get real-time feedback from Process, use OutputDataReceived and ErrorDataReceived events. Something like this:

process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;

process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;

process.OutputDataReceived += Process_OutputDataReceived;
process.ErrorDataReceived += Process_ErrorDataReceived;

process.Start();

process.BeginOutputReadLine();
process.BeginErrorReadLine();

process.WaitForExit();

private void Process_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
    if (e.Data == null) return;
    log("ERROR: " + e.Data);
}

private void Process_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
    if (e.Data == null) return;
    log(e.Data);
}
Avo Nappo
  • 620
  • 4
  • 9
  • If the exe file which runs in CMD starts, the procedure is like c#->CMD-> opening the exe file isn't it? In this case due to the code process.WaitForExit(); form gets into hanging status since the EXE program is the crypto currency mining program thus it produces live time data. Sorry for my lack of knowledge but please help – AJ Studio Jun 01 '18 at 11:04
  • Run the method that runs the process in a separate thread - you can search for Task.Factory.StartNew or async await pattern. The former i find conceptually simpler, but it's a matter of taste mostly. What you have to remember with parallel threads, is that you cannot access GUI objects directly from non-main threads. So, in your code the richTextBox1.Text = sb.ToString(); assignment would produce cross-thread violation error. To get around that, look up examples of how Invoke and BeginInvoke methods can be used for cross-thread UI updates. – Avo Nappo Jun 01 '18 at 12:44