Loading

Safe handle has been clos

  1.         public static void ConnectWithTimeout(this TcpClient tc, string host, int port)
  2.         {
  3.             if (tc == null)
  4.                 throw new ArgumentNullException("tc");
  5.             if (string.IsNullOrEmpty(host))
  6.                 throw new ArgumentNullException("host");
  7.  
  8.             var ar = tc.BeginConnect(host, port, null, null);
  9.             var wh = ar.AsyncWaitHandle;
  10.             try
  11.             {
  12.                 if (!ar.AsyncWaitHandle.WaitOne(tc.SendTimeout, false))
  13.                 {
  14.                     tc.Close();
  15.                     throw new TimeoutException();
  16.                 }
  17.                 tc.EndConnect(ar);
  18.             }
  19.             finally
  20.             {
  21.                 wh.Close();
  22.             }
  23.         }
  24.  
  25.  
  26. ===============================================
  27.  
  28.  
  29. var tc = new TcpClient();
  30. tc.SendTimeout = 1000;
  31. Helpers.ConnectWithTimeout(tc, "192.168.2.100", 6667);
  32.  
  33. System.TimeoutException: The operation has timed out.
  34.   at Local.Helpers.ConnectWithTimeout (System.Net.Sockets.TcpClient tc, System.String host, System.Int32 port) [0x0004d] in <84d4313427c14d0ebd16f3f379c8b3ed>:0
  35.   at <InteractiveExpressionClass>.Host (System.Object& $retval) [0x00000] in <839313ffb5aa47669729c09c35311b92>:0
  36.   at Mono.CSharp.Evaluator.Evaluate (System.String input, System.Object& result, System.Boolean& result_set) [0x00038] in <7a5ad0481e604e058be9486824118d15>:0
  37.   at Mono.CSharpShell.Evaluate (System.String input) [0x00000] in <23d81e17b05746d89659f83b8831d77b>:0
  38.  
  39. [ERROR] FATAL UNHANDLED EXCEPTION: System.ObjectDisposedException: Safe handle has been closed
  40.   at System.Runtime.InteropServices.SafeHandle.DangerousAddRef (System.Boolean& success) <0x7f7471aad510 + 0x000e8> in <7b0d87324cab49bf96eac679025e77d1>:0
  41.   at System.Threading.NativeEventCalls.SetEvent (Microsoft.Win32.SafeHandles.SafeWaitHandle handle) <0x7f7471a1ca20 + 0x0002a> in <7b0d87324cab49bf96eac679025e77d1>:0
  42.   at System.Threading.EventWaitHandle.Set () <0x7f7471a11c40 + 0x00009> in <7b0d87324cab49bf96eac679025e77d1>:0
  43.   at (wrapper remoting-invoke-with-check) System.Threading.EventWaitHandle.Set()
  44.   at System.IOAsyncResult.set_IsCompleted (System.Boolean value) [0x0001e] in <eeec8e88b1fc462e84ade7bf334d0c97>:0
  45.   at System.Net.Sockets.SocketAsyncResult.Complete () [0x00031] in <eeec8e88b1fc462e84ade7bf334d0c97>:0
  46.   at System.Net.Sockets.SocketAsyncResult.Complete (System.Exception e) [0x00007] in <eeec8e88b1fc462e84ade7bf334d0c97>:0
  47.   at System.Net.Sockets.Socket+<>c.<.cctor>b__313_4 (System.IOAsyncResult ares) [0x000d8] in <eeec8e88b1fc462e84ade7bf334d0c97>:0
  48.   at System.IOSelectorJob.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem () [0x00000] in <eeec8e88b1fc462e84ade7bf334d0c97>:0
  49.   at System.Threading.ThreadPoolWorkQueue.Dispatch () [0x00074] in <7b0d87324cab49bf96eac679025e77d1>:0
  50.   at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback () <0x7f7471a192f0 + 0x00005> in <7b0d87324cab49bf96eac679025e77d1>:0

Comments