public static bool Connect(Socket socket, IPAddress[] addresses, int port, int timeout) { using (var waitAsyncResult = new WaitAsyncResult((w, r) => { var s = (Socket)r.AsyncState; var succeed = false; try { s.EndConnect(r); succeed = !w.TimedOut; } catch (SystemException sex) { if (g_traceInfo.IsWarningEnabled) { TraceHelper.TraceWarning(g_traceInfo, sex.Message); } } if (!succeed) { s.Close(); } return(succeed); })) { socket.BeginConnect(addresses, port, waitAsyncResult.Callback, socket); return(waitAsyncResult.Wait(timeout)); } }
public static IPAddress[] GetHostAddresses(string hostNameOrAddress, int timeout) { IPAddress[] addresses = null; using (var waitAsyncResult = new WaitAsyncResult((w, r) => { try { addresses = Dns.EndGetHostAddresses(r); return(true); } catch (SystemException sex) { if (g_traceInfo.IsWarningEnabled) { TraceHelper.TraceWarning(g_traceInfo, sex.Message); } return(false); } })) { Dns.BeginGetHostAddresses(hostNameOrAddress, waitAsyncResult.Callback, null); waitAsyncResult.Wait(timeout); return(addresses); } }
public static void Wait_Completed(bool callbackSucceed) { // Arrange var waitAsyncResult = new WaitAsyncResult((w, r) => callbackSucceed); // Act waitAsyncResult.Callback(null); var result = waitAsyncResult.Wait(0); // Assert Assert.Equal(callbackSucceed, result); Assert.False(waitAsyncResult.TimedOut); Assert.Equal(callbackSucceed, waitAsyncResult.Succeed); }