Console Redirection

Everything todo with programming goes HERE.
Post Reply
SoulOfSet
On Lightcycle Grid
Posts: 30
Joined: Tue Feb 22, 2011 3:54 am
Contact:

Console Redirection

Post by SoulOfSet »

Im trying to redirect console output from the arma dedicated into a windows form application written in C#. I'm having a hell of a time with it and cant seem to quite get it right.

This is what I have so far. It just hangs the program though when it's called though.

Code: Select all

public static void startArma()
        {
            armaServer.StartInfo.UseShellExecute = false;
            armaServer.StartInfo.ErrorDialog = false;

            armaServer.StartInfo.RedirectStandardOutput = true;
            armaServer.StartInfo.CreateNoWindow = true;

            armaServer.StartInfo.FileName = @"C:\Program Files\Armagetron Advanced Dedicated\armagetronad_dedicated.exe";
            armaServer.Start();

            StreamReader armaOutputReader = ControlFunctions.armaServer.StandardOutput;

            string armaOutput = armaOutputReader.ReadToEnd();
        }
User avatar
AI-team
Shutout Match Winner
Posts: 1020
Joined: Tue Jun 23, 2009 6:17 pm
Location: Germany/Munich
Contact:

Re: Console Redirection

Post by AI-team »

May I ask why you want to do that?
  
 
"95% of people believe in every quote you post on the internet" ~ Abraham Lincoln
 
 
SoulOfSet
On Lightcycle Grid
Posts: 30
Joined: Tue Feb 22, 2011 3:54 am
Contact:

Re: Console Redirection

Post by SoulOfSet »

Oh dear. Should of explained that. I'm making a dedicated server UI. Kinda my intro to C# project. I've got most of it done besides this little roadblock :(
User avatar
AI-team
Shutout Match Winner
Posts: 1020
Joined: Tue Jun 23, 2009 6:17 pm
Location: Germany/Munich
Contact:

Re: Console Redirection

Post by AI-team »

check this out, I'm pretty sure it'll help you
http://stackoverflow.com/questions/2063 ... ut-results
  
 
"95% of people believe in every quote you post on the internet" ~ Abraham Lincoln
 
 
User avatar
Tank Program
Forum & Project Admin, PhD
Posts: 6711
Joined: Thu Dec 18, 2003 7:03 pm

Re: Console Redirection

Post by Tank Program »

Not sure off the top of my head, but doesn't ReadToEnd imply that it will load all output up until the process terminates? I think you want something that reads only until a new line character. This'll need to be in some sort of loop, e.g.:

Code: Select all

While (Tron is Running) {
    Read a line from the console
    Process based on that line
}
Is there a BufferedStreamReader? That would be the right direction to look in I think.
Image
SoulOfSet
On Lightcycle Grid
Posts: 30
Joined: Tue Feb 22, 2011 3:54 am
Contact:

Re: Console Redirection

Post by SoulOfSet »

Tank Program wrote:Not sure off the top of my head, but doesn't ReadToEnd imply that it will load all output up until the process terminates? I think you want something that reads only until a new line character. This'll need to be in some sort of loop, e.g.:

Code: Select all

While (Tron is Running) {
    Read a line from the console
    Process based on that line
}
Is there a BufferedStreamReader? That would be the right direction to look in I think.
That would explain why it hangs when the program starts and continues when I manually end the process. Thanks for that. I'll look into what you've said and communicate my findings :3.
User avatar
AI-team
Shutout Match Winner
Posts: 1020
Joined: Tue Jun 23, 2009 6:17 pm
Location: Germany/Munich
Contact:

Re: Console Redirection

Post by AI-team »

Or use EventHandlers ;)

Code: Select all

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Process Proc = new Process();
            Proc.StartInfo.FileName = "C:\\Users\\Niklas\\Desktop\\tron\\0.4-queue+timeleft+scoreboard\\build\\dist\\armagetronad_dedicated.exe";
            Proc.StartInfo.UseShellExecute = false;
            Proc.StartInfo.RedirectStandardOutput = true;
            Proc.StartInfo.RedirectStandardError = true;

            Proc.OutputDataReceived += new DataReceivedEventHandler(receivedData);
            Proc.ErrorDataReceived += new DataReceivedEventHandler(receivedError);

            Console.WriteLine("starting the process...");
            Proc.Start();

            Proc.BeginOutputReadLine();
            Proc.BeginErrorReadLine();

            Proc.WaitForExit();
        }

        static void receivedData(object o, DataReceivedEventArgs data)
        {
            if (data.Data != null)
            {
                Console.WriteLine(data.Data);
                Console.Out.Flush();
            }
        }

        static void receivedError(object o, DataReceivedEventArgs error)
        {
            if (error.Data != null)
            {
                Console.Error.WriteLine(error.Data);
                Console.Error.Flush();
            }
        }
    }
}
  
 
"95% of people believe in every quote you post on the internet" ~ Abraham Lincoln
 
 
SoulOfSet
On Lightcycle Grid
Posts: 30
Joined: Tue Feb 22, 2011 3:54 am
Contact:

Re: Console Redirection

Post by SoulOfSet »

Yeah I ended up with something similar to that. Im just trying now to get it to work with the windows forms. Weird protection thing.
Post Reply