标签:ipad close handle completed com one else int lin
如题
原理:异步连接,获取状态。阻止主进程等待异步进程返回。指定异步进程超时时间,这段时间内,如果异步连接没有返回,则以未完成状态返回,主进程继续。
根据对获取到的状态的识别,来判断是超时还是收到回应而使进程继续。再分别处理。
代码:
static void Main(string[] args) { for (int i = 20; i <= 25; i++) { TcpClient tcp = new TcpClient(); IAsyncResult async = tcp.BeginConnect(IPAddress.Parse("120.27.234.XXX"), i, null, null); async.AsyncWaitHandle.WaitOne(1000); if (async.IsCompleted) { Console.WriteLine($"{i} is open."); } else { Console.WriteLine($"{i} is closed."); ; } tcp.Close(); } Console.WriteLine("ok"); Console.ReadKey(); }
标签:ipad close handle completed com one else int lin
原文地址:https://www.cnblogs.com/wanjinliu/p/12669556.html