0

基本上我试图启动一个进程并获得输出。该程序是 minerd.exe(比特币 cpu 矿工)https://github.com/pooler/cpuminer供参考我尝试过这样的事情:

private  void StartMining()
        {

            Process p = new Process();
            StreamWriter sw;
            StreamReader sr;
            StreamReader err;
            ProcessStartInfo psI = new ProcessStartInfo(@"miners\minerd.exe");
            psI.Arguments = "-o " + miner.PoolIp + ":" + miner.PoolServerPort + " -O " + miner.PoolUsername + ":" + miner.PoolPassword;
            psI.UseShellExecute = false;
            psI.RedirectStandardInput = true;
            psI.RedirectStandardOutput = true;
            psI.RedirectStandardError = true;
            psI.CreateNoWindow = true;
            p.StartInfo = psI;
            p.OutputDataReceived += new DataReceivedEventHandler(proc_OutputDataReceived);

            p.ErrorDataReceived += new DataReceivedEventHandler(proc_ErrorDataReceived);
            p.Start();


            p.BeginOutputReadLine();

        }

        private void proc_ErrorDataReceived(object sender, DataReceivedEventArgs e)
        {
            WriteLogWindow(e.Data, Color.Green);
        }

        private void proc_OutputDataReceived(object sender, DataReceivedEventArgs e)
        {
            WriteLogWindow(e.Data, Color.Red);
        }
 private delegate void DelegateInsertNewLineToLogRichTextBox(string text, Color color);
        void WriteLogWindow(string text, Color color)
        {
            if (this.richTextBox1.InvokeRequired)
            {
                DelegateInsertNewLineToLogRichTextBox method = new DelegateInsertNewLineToLogRichTextBox(InvokedInsertNewLineToLgoRichTextBox);
                Invoke(method, new object[2] { text, color });

            }
            else
            {
                InvokedInsertNewLineToLgoRichTextBox(text, color);

            }
        }
        private void InvokedInsertNewLineToLgoRichTextBox(string message, Color color)
        {
            richTextBox1.Font = new Font("Arial", 8F, FontStyle.Bold);
            richTextBox1.SelectionColor = color;
            richTextBox1.SelectedText = Environment.NewLine + message + "\t" + DateTime.Now.ToString("HH:mm:ss d/M/yyyy");
            richTextBox1.ScrollToCaret();
        }

矿工似乎在工作,但我没有得到任何输出。

提前感谢您的任何建议

4

2 回答 2

2

你没有打电话BeginErrorReadLine,所以你不会得到任何错误结果——也许所有的输出都是通过错误流报告的?

于 2013-08-12T11:06:57.433 回答
1

如果你想得到一个程序的输出,你需要做

p.WaitForExit();
int retVal = p.ExitCode;

请注意,这WaitForExit将停止您的线程,因此您不应该在 UI 线程上使用它

另请注意,ExitCode将在其函数中返回“矿工”返回的内容main,而不是“矿工”的实际输出 ( WriteLine)

于 2013-08-12T10:57:13.733 回答