C# 获取minerd.exe的输出
基本上,我试图启动一个进程并获得输出。该程序为minerd.exe(比特币cpu miner)以供参考 我试过这样的方法:C# 获取minerd.exe的输出,c#,C#,基本上,我试图启动一个进程并获得输出。该程序为minerd.exe(比特币cpu miner)以供参考 我试过这样的方法: private void StartMining() { Process p = new Process(); StreamWriter sw; StreamReader sr; StreamReader err; ProcessSta
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();
}
矿工似乎在工作,但我没有收获
如果您想获得所需程序的输出,请提前感谢您的建议
p.WaitForExit();
int retVal = p.ExitCode;
请注意,WaitForExit
将暂停线程,因此您不应该在UI线程上执行它
还请注意,如果您想获得需要执行的程序的输出,则
ExitCode
将在其main
函数中返回“minerd”返回的内容,而不是“minerd”的实际输出(WriteLine
)
p.WaitForExit();
int retVal = p.ExitCode;
请注意,WaitForExit
将暂停线程,因此您不应该在UI线程上执行它
还请注意,
ExitCode
将在其main
函数中返回“minerd”返回的内容,而不是您未调用的“minerd”的实际输出(WriteLine
),因此您将不会得到任何错误结果-也许所有输出都是通过错误流报告的?您未调用,因此,您不会得到任何错误结果-可能所有输出都是通过错误流报告的?我需要异步输出这是异步的@adisba。你可能是想让它同步?如果你使用我在UI线程中给你的代码,那么它将是同步的@adisba。但是,它会使程序看起来卡住,因此如果minerd需要时间,我建议您将其设置为异步,并向用户显示一个进度条或其他信息,因为minerd正在长时间运行,因此我将在很长时间后获得输出,然后创建一个新线程来运行它,并等待它结束。当它最终使用invoke干扰UI线程并重新启用我需要的所有输出时,这是异步的@adisba。你可能是想让它同步?如果你使用我在UI线程中给你的代码,那么它将是同步的@adisba。但是,它会使程序看起来卡住,因此如果minerd需要时间,我建议您将其设置为异步,并向用户显示一个进度条或其他信息,因为minerd正在长时间运行,因此我将在很长时间后获得输出,然后创建一个新线程来运行它,并等待它结束。当它最终使用invoke来干扰UI线程并重新启用所有东西时,它的输出似乎实际上在错误流中,即使程序工作正常,所以现在我从错误流中获得了输出。这有点奇怪。但是ThanksIt似乎输出它实际上在错误流中,即使程序工作正常,所以现在我从错误流中得到输出。这有点奇怪。但是Thanks@LovesTha:对,这解释了一切-基本上调用BeginErrorReadLine
以及BeginOutputReadLine
应该可以工作。@LovesTha:对,这解释了一切-基本上调用BeginErrorReadLine
以及BeginOutputReadLine
应该可以工作。