Note that there are some explanatory texts on larger screens.

plurals
  1. POSocketAsyncEventArgs.Completed doesn't fire in Windows 8
    text
    copied!<p>When I compile this code on a machine with Windows 7 Ultimate and .NET 4 installed, it works just fine but when I try it on one with Windows 8 RTM and .NET 4.5 installed, Complete event never fires.</p> <pre><code>class Program { private static Socket _Socket = new Socket( AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); private static void Main(string[] args) { _Socket.Bind(new IPEndPoint(IPAddress.Any, 5012)); _Socket.Listen(100); var arguments = new SocketAsyncEventArgs(); arguments.Completed += OnAccepted; Accept(arguments); Console.ReadLine(); } private static void Accept(SocketAsyncEventArgs args) { args.AcceptSocket = null; if (!_Socket.AcceptAsync(args)) OnAccepted(null, args); } private static void OnAccepted(object sender, SocketAsyncEventArgs e) { Console.WriteLine("Accepted."); Accept(e); } } </code></pre> <p>The interesting thing here is if I put a breakpoint at this line and debug it:</p> <pre><code>var arguments = new SocketAsyncEventArgs(); </code></pre> <p>And connect this server using <a href="http://www.hw-group.com/products/hercules/index_en.html" rel="nofollow noreferrer" title="Hercules SETUP Utility">Hercules</a> before continuing execution, it works like a charm. I do this at the start and then magically, OnAccepted gets called and writes "Accepted." to the console on every single connection. I use the same code and same program (Hercules) on the machine with Windows 7 and .NET 4 but it always works.</p> <ul> <li>Am I doing something wrong?</li> <li>If not, is it a known bug of my OS or .NET Framework version 4.5? </li> <li>Can anyone reproduce this?</li> </ul> <p><strong>Edit</strong>: Both operating systems are 64 bit.<br> <strong>Edit 2</strong>: I reported this as a bug on Microsoft Connect, <a href="https://connect.microsoft.com/VisualStudio/feedback/details/759913/socketasynceventargs-completed-doesnt-fire-in-net-framework-4-5" rel="nofollow noreferrer" title="SocketAsyncEventArgs.Completed doesn&#39;t fire in .NET Framework 4.5">here</a>.<br> <strong>Edit 3</strong>: Found a workaround and post it to Connect (Simply by creating a fake, first connection).<br> <strong>Edit 4</strong>: If anyone can reproduce this, please join the issue in Connect.<br> <strong>Edit 5</strong>: I saw the <a href="https://stackoverflow.com/q/12464185/704144">question Thomas has mentioned</a> and I tested whether <code>Console.ReadLine</code> was causing this or not. Turned out it was. If I add <code>Thread.Sleep(3000)</code> <strong>before</strong> my <code>Console.ReadLine</code> call and make a connection attempt in 3 seconds after I run the program, it works like a charm. Again, the odd thing is that I need to do this only once before calling <code>Console.ReadLine</code>. If I make one connection before calling <code>Console.ReadLine</code> then every consecutive connection works, even after <code>Console.ReadLine</code> is called. I'll mention this in the Conect page.<br> <strong>Edit 6</strong>: I added the link to the other question to the Connect page and added another workaround that involves calling <code>Thread.Sleep</code> before calling <code>Console.ReadLine</code> like I mentioned in the above edit.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload