[英]C#: Why is my Process that is running Java.exe working perfectly, but the window isn't returning ANY output?
我创建了一个针对Java.exe文件的进程。 该进程应运行Java.jar服务器文件,并继续运行,提供输出反馈,直到服务器崩溃或我强行关闭它。 一切正常..服务器正在运行..当我将UseShellExecute设置为True时,我可以看到黑色CMD窗口返回所有输出。 但是,当我将其设置为false并重定向输出时...窗口完全空白(服务器仍然在运行)并且OutputDataReceived事件根本不会触发(我想我得到它一次但是那时我关闭窗口似乎args是空的)并且据我所知,StandardOutput.ReadToEnd()也没有返回任何东西。 为什么这个过程没有返回任何输出反馈? 这是我的代码:
gameServerProcess = new Process();
gameServerProcess.StartInfo.UseShellExecute = false;
gameServerProcess.StartInfo.RedirectStandardOutput = true;
gameServerProcess.StartInfo.RedirectStandardInput = true;
gameServerProcess.EnableRaisingEvents = true;
gameServerProcess.Exited += new EventHandler(gameServer_WindowExit);
window = new ServerWindow();
gameServerProcess.OutputDataReceived += new DataReceivedEventHandler(window.server_recievedOutputStream);
window.Show();
gameServerProcess.StartInfo.FileName = @"D:\Program Files\Java\jdk1.6.0_12\bin\java.exe";
gameServerProcess.StartInfo.WorkingDirectory = @"D:\Users\Zack\Desktop\ServerFiles\gameserver";
gameServerProcess.StartInfo.Arguments = @"-Xmx1024m -cp ./../libs/*;l2jserver.jar net.sf.l2j.gameserver.GameServer";
gameServerProcess.Start();
gameServerProcess.BeginOutputReadLine();
我的'window'表单类代码应该接收带有输出数据的DataReceivedEventArgs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
namespace MWRemoteServer
{
public partial class ServerWindow : Form
{
private delegate void WriteOutputDelegate(string output);
private WriteOutputDelegate writeOutput;
public ServerWindow()
{
InitializeComponent();
logBox.BackColor = Color.White;
logBox.ForeColor = Color.Black;
writeOutput = new WriteOutputDelegate(write);
}
public void server_recievedOutputStream(object sender, DataReceivedEventArgs args)
{
MessageBox.Show("Called window output!");
if (args.Data != null)
{
BeginInvoke(writeOutput, new object[] { args.Data.ToString() });
}
}
private void write(string output)
{
logBox.AppendText(output + Environment.NewLine);
}
}
}
再次,该过程完全正常,并且UseShellExecute设置为true,我可以看到它提供了我需要抓取的所有信息。 但是,当我将其设置为false时,它的空白似乎无法获取任何输出数据!
非常感谢你...我已经在这里待了几个小时......
你确定它写入stdout吗? 你试过检查stderr吗? ( RedirectStandardError
/ StandardError
/ ErrorDataReceived
/ BeginErrorReadLine
)。
如果它直接写入显示缓冲区(而不是stderr / stdout),那么如果没有大量工作可能是不可能的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.