简体   繁体   English

C#:为什么运行Java.exe的我的Process工作正常,但是窗口没有返回任何输出?

[英]C#: Why is my Process that is running Java.exe working perfectly, but the window isn't returning ANY output?

I created a Process that is targeted to the Java.exe file. 我创建了一个针对Java.exe文件的进程。 The process should run a Java.jar server file, and continue to run giving output feedback until the server crashed or I forcefully close it down. 该进程应运行Java.jar服务器文件,并继续运行,提供输出反馈,直到服务器崩溃或我强行关闭它。 Everything works fine.. the server is running.. and when I set UseShellExecute to True I can see the black CMD window returning all the output. 一切正常..服务器正在运行..当我将UseShellExecute设置为True时,我可以看到黑色CMD窗口返回所有输出。 But when I set it to false and redirect the output... the Window is completely blank ( the server is still running though ) and the OutputDataReceived event doesn't fire at all ( I think I got it to once but that was when I closed the window it seemed the args was empty ) and as far as I can see StandardOutput.ReadToEnd() isnt returning anything either. 但是,当我将其设置为false并重定向输出时...窗口完全空白(服务器仍然在运行)并且OutputDataReceived事件根本不会触发(我想我得到它一次但是那时我关闭窗口似乎args是空的)并且据我所知,StandardOutput.ReadToEnd()也没有返回任何东西。 Why is this Process not returning any Output feedback?? 为什么这个过程没有返回任何输出反馈? Here is my code: 这是我的代码:

    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();

And my 'window' form class code which should receive the DataReceivedEventArgs with the output data: 我的'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);
        }
    }
}

Again the process works completely fine and with the UseShellExecute set to true I can see it is providing all information I need to grab. 再次,该过程完全正常,并且UseShellExecute设置为true,我可以看到它提供了我需要抓取的所有信息。 But when I set that to false its blank and I cant seem to grab any output data! 但是,当我将其设置为false时,它的空白似乎无法获取任何输出数据!

Thank you so much in advance... I've been at this for hours and hours... 非常感谢你...我已经在这里待了几个小时......

Are you sure it writes to stdout? 你确定它写入stdout吗? Have you tried checking stderr? 你试过检查stderr吗? ( RedirectStandardError / StandardError / ErrorDataReceived / BeginErrorReadLine ). RedirectStandardError / StandardError / ErrorDataReceived / BeginErrorReadLine )。

If it writes directly to the display buffer (instead of stderr/stdout) then it might not be possible without a lot of work. 如果它直接写入显示缓冲区(而不是stderr / stdout),那么如果没有大量工作可能是不可能的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM