Valhalla Legends Archive

Programming => General Programming => .NET Platform => Topic started by: shout on January 16, 2005, 09:07 AM

Title: [c#]Keeping a console window open
Post by: shout on January 16, 2005, 09:07 AM
Is there a way to keep a console window open? I wanted to create a bot that runs in a console window so it takes less system resources, but when I try to make one it closes when it gets to a point when nothing can be done until an event.
Title: Re: [c#]Keeping a console window open
Post by: Barumonk on January 16, 2005, 11:47 AM
There are probably more elegant ways to handle this but oh well, here goes...

private bool m_Active = true;

...

//initialize everything

while (m_Active)
{
    //handle something here, be it sockets or console commands

    //m_Active = false; to shutdown
}

//dispose unmanaged resources, shut down the bot
Title: Re: [c#]Keeping a console window open
Post by: MyndFyre on January 16, 2005, 01:34 PM

do
{

// stuff

} while (Console.ReadLine().ToLower() != "exit");


will keep a console window open until the user enters "exit".

My console connection driver function is roughly this:


// I've already initialized the settings variable.
// I also set a using Cons = System.Console;, since the namespace is JinxBot.Console.
ConnectionProtocol con = Connections.GetConnection(settings);

con.EventHost.RegisterEvent(EventType.PacketTransmitted, pktTrans, Priority.Normal);
con.EventHost.RegisterEvent(EventType.ConnectionError, conError, Priority.Normal);
con.EventHost.RegisterEvent(EventType.StatusChanged, status, Priority.Normal);

string sSend = "connect";
con.Connect();
while (sSend != "exit")
{
sSend = Cons.ReadLine();
if (sSend != "exit")
con.Send(sSend);
}

con.EventHost.UnregisterEvent(EventType.PacketTransmitted, pktTrans, Priority.Normal);
con.EventHost.UnregisterEvent(EventType.ConnectionError, conError, Priority.Normal);
con.EventHost.UnregisterEvent(EventType.StatusChanged, status, Priority.Normal);

Cons.WriteLine("Press <ENTER> to exit.");
Cons.Read();
Title: Re: [c#]Keeping a console window open
Post by: shout on January 16, 2005, 03:15 PM
So I have to have some kind of loop going to keep the console window open? :(

Oh well.
Title: Re: [c#]Keeping a console window open
Post by: dxoigmn on January 16, 2005, 05:24 PM
Quote from: shout on January 16, 2005, 03:15 PM
So I have to have some kind of loop going to keep the console window open? :(

Oh well.

You don't have to.  You can have some sort of ManualReset event that is set when the bot disconnects.
Title: Re: [c#]Keeping a console window open
Post by: shout on January 17, 2005, 07:04 AM
Quote from: dxoigmn on January 16, 2005, 05:24 PM
You don't have to. You can have some sort of ManualReset event that is set when the bot disconnects.

Could you explian this? Won't you need some sort of loop running anyway that exits when the ManualReset event is set?
Title: Re: [c#]Keeping a console window open
Post by: dxoigmn on January 17, 2005, 01:18 PM
Quote from: shout on January 17, 2005, 07:04 AM
Quote from: dxoigmn on January 16, 2005, 05:24 PM
You don't have to. You can have some sort of ManualReset event that is set when the bot disconnects.

Could you explian this? Won't you need some sort of loop running anyway that exits when the ManualReset event is set?

Depends on how you have your classes setup.  If you use events (delegates in C# I think), then you can have the event set in your Disconnected event, and in your main, you can have it WaitOne(). 

Example in VB.NET:

Module Main
    Private WithEvents bncsClient as BncsClient
    Private disconnectedEvent as ManualResetEvent

    Public Sub Main()
        bncsClient.Connect()
        disconnectedEvent.WaitOne()
    End Sub

    Private Sub bncsClient_Disconnected(ByVal sender As Object, ByVal e As System.EventArgs) Handles bncsClient.Disconnected
        disconnectedEvent.Set()
    End Sub
End Module
Title: Re: [c#]Keeping a console window open
Post by: MyndFyre on January 17, 2005, 03:24 PM
You can really just use Console.Read() or Console.ReadLine() to keep the window open until the user hits enter.  This allows other threads to continue running and only blocks the main thread, which is fine as long as you're using multiple threads from the thread pool (typical if you're using events).

Using a do...while(Console.ReadLine() != "exit") isn't going to utilize 100% CPU time; Console.Read() and Console.ReadLine() are blocking until there is user input, so you don't need to worry about using the CPU up with a loop.  It is generally considered elegant and typically used.  I don't know why you guys are making this so complicated.
Title: Re: [c#]Keeping a console window open
Post by: dxoigmn on January 17, 2005, 04:07 PM
Quote from: MyndFyre on January 17, 2005, 03:24 PM
You can really just use Console.Read() or Console.ReadLine() to keep the window open until the user hits enter.  This allows other threads to continue running and only blocks the main thread, which is fine as long as you're using multiple threads from the thread pool (typical if you're using events).

Using a do...while(Console.ReadLine() != "exit") isn't going to utilize 100% CPU time; Console.Read() and Console.ReadLine() are blocking until there is user input, so you don't need to worry about using the CPU up with a loop.  It is generally considered elegant and typically used.  I don't know why you guys are making this so complicated.

I don't like the user being able to type anything in as it messes up the output.  But if you consider that elegant, then so be it.
Title: Re: [c#]Keeping a console window open
Post by: shout on January 18, 2005, 10:49 AM
I have been thinking, because I do not want the user to input things (it would be controlled from bnet ONLY), I could run a program with no user interface at all, and have a .log file or something.

Mabye have a console window that opens when an error is encountered, then exits but the orginal thread keeps running... But that could get kind of messy.