2

I have been having playing with implementing some socket code to see if it fits my needs and so use the sample code @spender kindly added to this question.

If I run this on the main thread it works as expected but when I invoke it on a background thread it never gets awoken from its sleep when a client attempts to connect, my thread spawn is as below:

_Thread = new Thread(new ThreadStart(StartListening));
_Thread.Name = "ThreadForSocket";
_Thread.IsBackground = true;
_Thread.Start();


private void StartListening()
{
    new AsyncSocketListener().StartListening(InitializeEndPoint());
}

public class AsyncSocketListener : IDisposable
{
    public void StartListening(IPEndPoint endPoint)
    {
        try
        {
            var socket = new Socket(endPoint.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
            socket.Bind(endPoint);
            socket.Listen(10);

            while (true)
            {
                string info = string.Format("{0} Waiting for a connection...", DateTime.Now.ToString("HH:mm.ss"));
                Controller.StatusSignal.Reset();

                Console.WriteLine(info);
                Debug.WriteLine(info);

                socket.BeginAccept(new     AsyncCallback(SocketListener.AcceptCallback), socket);

                Controller.StatusSignal.WaitOne();
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
        }

        Console.WriteLine("Closing the listener...");
    }

The thread is still present in the Threads Window and is in the expected state so I'm at a loss as to why it refuses to wake up on client connection.

Should that be possible? I read the socket msdn page and it appears to suggest it should be OK for a background thread.

Community
  • 1
  • 1
lazarus
  • 371
  • 2
  • 10
  • You need to add more code to show how to reproduce the problem. – Scott Chamberlain Jan 01 '16 at 16:39
  • The actual socket code is linked to directly above. The delegate above 'StartListening' merely calls this code: http://stackoverflow.com/a/12631467/181221 – lazarus Jan 01 '16 at 17:11
  • Code must be in questions themseves, not in links to other websites or other questions on this website. Also there is no StartListening method in the other question, you at least should have put that method in. – Scott Chamberlain Jan 01 '16 at 17:23
  • added code directly rather thank linked, thanks – lazarus Jan 01 '16 at 17:48

1 Answers1

0

(After too much hair-pulling and a process of elimination)

The code is fine, (Windows) Firewall had blocked the binary using the background thread (without notifying, despite its settings) but had allowed the non-threaded version thru, hence why I originally thought it was a code issue.

So, yes, the async pattern shown above is perfectly fine running on a background thread as shown, and of course its quite a nice way of using it as you are simply sleeping the bg thread and your main (eg. UI) thread can operate normally.

lazarus
  • 371
  • 2
  • 10