-
-
Save Inok/9b17282532bca8a01cc5fc0ab9872433 to your computer and use it in GitHub Desktop.
UnobservedTaskException with sockets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static void Main(string[] args) | |
{ | |
TaskScheduler.UnobservedTaskException += (sender, args) => | |
{ | |
Console.WriteLine("TaskScheduler.UnobservedTaskException event:"); | |
Console.WriteLine(args.Exception.ToString()); | |
args.SetObserved(); | |
}; | |
DoConnect(); | |
GC.Collect(2, GCCollectionMode.Forced, true); | |
GC.Collect(2, GCCollectionMode.Forced, true); | |
GC.WaitForPendingFinalizers(); | |
} | |
private static void DoConnect() | |
{ | |
var address = IPAddress.Parse("127.0.0.1"); | |
var socket = new Socket(address.AddressFamily, SocketType.Stream, ProtocolType.Tcp); | |
var result = socket.BeginConnect(new IPEndPoint(address, 12345), null, null); | |
result.AsyncWaitHandle.WaitOne(5_000, true); | |
if (!socket.Connected) | |
{ | |
Console.WriteLine("socket.Connected == false"); | |
socket.Close(); | |
} | |
else | |
{ | |
Console.WriteLine("socket.Connected == true"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment