0 votes
by (150 points)
edited

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
by (70.2k points)
edited
 
Best answer

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

0 votes
by (70.2k points)
edited

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());
});
...