Async use of Telnet

0 votes
asked Jan 10, 2013 by MasTool (150 points)
edited Jul 29, 2013

Hello, I'm testing the Telnet component with the TerminalControl, and it seems that the Bind method is blocking. So if I try to connect to a non existing address, the program UI hangs for a long time.

All your examples are sync, so I could not find a way to achieve it. Kind regards, Boris

2 Answers

0 votes
answered Jul 29, 2013 by Lukas Matyska (57,550 points)
edited Jul 29, 2013
 
Best answer

From version 2013 R1 (build number 4959) you can use public method BindAsync.

0 votes
answered Jan 11, 2013 by Lukas Matyska (57,550 points)
edited Jan 11, 2013

Thank you for your hint. The BindAsync seems to be a useful method. We will probably add it.

In the meantime you can use one of asynchronous pattern such as:

BackgroundWorker:

BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += new DoWorkEventHandler(worker_DoWork);
worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
worker.RunWorkerAsync(new Telnet("server"));

void worker_DoWork(object sender, DoWorkEventArgs e)
{
    terminal.Bind((Telnet)e.Argument);
}

void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    if (e.Error != null)
        MessageBox.Show(e.Error.ToString());
}

Task in .NET 4.0:

Task task = new Task(() => terminal.Bind(new Telnet("server")));
task.ContinueWith((t) =>
{
    if (t.IsFaulted)
        MessageBox.Show(t.Exception.ToString());
});
task.Start();

Task in .NET 4.5:

await Task.Run(() => terminal.Bind(new Telnet("server"))).ContinueWith((t) =>
{
    if (t.IsFaulted)
        MessageBox.Show(t.Exception.ToString());
});
...