9swampy / Telnet

Published on Nuget at https://www.nuget.org/packages/Telnet

Home Page:http://www.nugetmusthaves.com/Package/Telnet

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

CancellationToken functionality at creating Client Instance

aviTwito opened this issue · comments

I have a method to check if device has a telnet server. I need to set timeout of 1 sec max, But the default behavior is much longer, I thought I could use the cancellation token to handle it, But it doesn't work. I either use it wrong or I don`t understand what the token is used for. I do get when debugging to the timer event and call Cancel() on the token.

` public async Task DeviceCanTelnet(string address)
{

        CancellationTokenSource cts = new CancellationTokenSource();

        try
        {
            System.Timers.Timer timer = new System.Timers.Timer();
            timer.Interval = 1000;
            timer.Start();
            timer.Elapsed += (s, e) => 
            { 
                cts.Cancel(); 
            };
            using (Client client = new Client(address, 23, cts.Token))
            {
                return true;
            }
        }
        catch(Exception e)
        {
            return false;
        }
  
    }
}`

Is there a way of solving it?

Is your calling code awaiting the async call to allow it to execute on background thread? As I've just mentioned on another query the Cancellation implementation is crying out for some love and attention but it's serviced my needs as is.