Valhalla Legends Forums Archive | .NET Platform | [c#]Keeping a console window open

AuthorMessageTime
shout
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.
January 16, 2005, 3:07 PM
Barumonk
There are probably more elegant ways to handle this but oh well, here goes...

[code]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[/code]
January 16, 2005, 5:47 PM
Myndfyr
[code]
do
{

// stuff

} while (Console.ReadLine().ToLower() != "exit");
[/code]

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

My console connection driver function is roughly this:

[code]
// 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();
[/code]
January 16, 2005, 7:34 PM
shout
So I have to have some kind of loop going to keep the console window open? :(

Oh well.
January 16, 2005, 9:15 PM
kamakazie
[quote author=shout link=topic=10211.msg95441#msg95441 date=1105910106]
So I have to have some kind of loop going to keep the console window open? :(

Oh well.
[/quote]

You don't have to.  You can have some sort of ManualReset event that is set when the bot disconnects.
January 16, 2005, 11:24 PM
shout
[quote author=dxoigmn link=topic=10211.msg95459#msg95459 date=1105917883]
You don't have to. You can have some sort of ManualReset event that is set when the bot disconnects.
[/quote]

Could you explian this? Won't you need some sort of loop running anyway that exits when the ManualReset event is set?
January 17, 2005, 1:04 PM
kamakazie
[quote author=shout link=topic=10211.msg95494#msg95494 date=1105967053]
[quote author=dxoigmn link=topic=10211.msg95459#msg95459 date=1105917883]
You don't have to. You can have some sort of ManualReset event that is set when the bot disconnects.
[/quote]

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

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:
[code]
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
[/code]
January 17, 2005, 7:18 PM
Myndfyr
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.
January 17, 2005, 9:24 PM
kamakazie
[quote author=MyndFyre link=topic=10211.msg95539#msg95539 date=1105997050]
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.
[/quote]

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.
January 17, 2005, 10:07 PM
shout
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.
January 18, 2005, 4:49 PM

Search