C #: Why is my process running on Java.exe excellent, but the window does not return ANY output?

I created a process aimed at the Java.exe file. The process should run the Java.jar server file and continue to work, giving output feedback until the server crashes or I close it. Everything is working fine .. the server is working .. and when I set UseShellExecute to True, I see that the black CMD window returns all the output. But when I set it to false and redirect the output ... The window is completely empty (the server is still working), and the OutputDataReceived event does not fire at all (I think I got it once, but that was when I closed the window, it seemed that the arguments were empty), and as far as I can see, StandardOutput.ReadToEnd () also returns nothing. Why does this process not return 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();

"" , 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, , !

... ...

+3
1

, stdout? stderr? (RedirectStandardError/StandardError/ErrorDataReceived/BeginErrorReadLine).

( stderr/stdout), .

+2

Source: https://habr.com/ru/post/1709076/


All Articles