• Welcome to Valhalla Legends Archive.
 

[c#]Keeping a console window open

Started by shout, January 16, 2005, 09:07 AM

Previous topic - Next topic

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.

Barumonk

#1
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

MyndFyre

#2

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();
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

shout

So I have to have some kind of loop going to keep the console window open? :(

Oh well.

dxoigmn

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.

shout

#5
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?

dxoigmn

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

MyndFyre

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.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

dxoigmn

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.

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.