views:

277

answers:

4

Hello All,

I am currently using a third party component to handle telnet connections in .NET. I want it to be synchronous where I send a command to the receiving telnet server and then I get the response back as text or byte array. Only problem is that the component is not set up to do that. The component allows me to send commands to the server, but the response is returned via a function handle. So in essence, I need a way to pause the application while the handler does it's processing. Here is an example of how I plan to get around that issue:

    static void Main(string[] args)
    {
        Telnet telCon = new Telnet();
        telCon.OnDataIn += new Telnet.OnDataInHandler(HandleDataIn);
        telCon.Connect(remoteHostStr);

        while (true) ;
    }

    public static void HandleDataIn(object sender, TelnetDataInEventArgs e)
    {
        string responseStr = e.Text;

        if (responseStr.Contains("Username:"))
        {
            ((Telnet)sender).Send(System.Text.ASCIIEncoding.ASCII.GetBytes(username));
        }
        else if (responseStr.Contains("Password:"))
        {
            ((Telnet)sender).Send(System.Text.ASCIIEncoding.ASCII.GetBytes(password));
        }
    }

The solution above will not work since the while will always run, but I will probably build a future version that uses some sort of global variable to track if the loop still needs to run. However, everything I have been taught about programming says this is very dirty. Can anyone think of another way around my dilemma?

Thanks, Chris

A: 

Your while loop:

while(true) ;

will drive CPU usage to 100% (well, 100% of 1 core on a multicore machine) and leave it there, permanently.

This will starve other processes of CPU power, and may prevent the Telnet component from working at all because you've bypassed the message pump.

There are better ways, but without more information on what you're doing, it will be hard to advise you.

To begin, do you want a WindowsForms/WPF/Console application?

[And please, use comments to answer, not Answers.]

Bevan
As a test, I'm looking to use it in a Console Application. Eventually it will be a part of an ASP.NET page, and the output from the telnet server will be wrote into the page response.
regex
A: 

In general, when you really need to wait, use a WaitHandle. In this case, a ManualResetEvent would probably be what you need.

Lucero
A: 

A better way would be to spawn the Telnet processing to another thread. That way you can get the main thread to wait for the telnet processing to complete.

Have a look here for some very good tutorials on threading.

caveman_dick
+1  A: 

Here is an example of using a ManualResetEvent to suspend execution (and delay program end) until your event handler says it's finished.

static ManualResetEvent finishGate;

static void Main(string[] args)
{
    finishGate = new ManualResetEvent(false); // initial state unsignaled

    Telnet telCon = new Telnet();
    telCon.OnDataIn += new Telnet.OnDataInHandler(HandleDataIn);
    telCon.Connect(remoteHostStr);

    finishGate.WaitOne(); // waits until the gate is signaled
}

public static void HandleDataIn(object sender, TelnetDataInEventArgs e)
{
    // handle event

    if (processingComplete)
        finishGate.Set(); // signals the gate
}

The WaitOne() method of ManualResetEvent also includes overrides that accept a timespan or number of milliseconds. It returns bool - true if it was signaled, false if it timed out. If you put that in a loop, you could have your main thread wake up every 30 seconds and perform some housekeeping tasks, but still have an instantaneous response when the gate is signaled.

David