title: Using TcpListener.AcceptSocket(); in a separate thread causes the thread to block?
body: <p>I have tried to work around this as well as debug but I'm at a loose end here :( is there any alternative to using this to check for a client connection? This code works fine in a console application so i am guessing the thread is being blocked, although it may be something else i can't see?</p>
  <pre><code>public partial class Form1 : Form
  {
      Socket s;
  </code></pre>
  <p>Declatation of socket.</p>
  <pre><code>private void startButton_Click(object sender, EventArgs e)
      {
          checkTimer.Enabled = true;
          if (bw.IsBusy != true)
          {
              bw.RunWorkerAsync();
          }
      }
  </code></pre>
  <p>Background thread to start on button press.</p>
  <pre><code>private void bw_DoWork(object sender, DoWorkEventArgs e)
  {
          BackgroundWorker worker = sender as BackgroundWorker;
  
          working();
  }
  </code></pre>
  <p>Thread runs the &quot;working&quot; method.</p>
  <pre><code> private void working()
   {
       if (threadFirstRun == true)
       {
              threadFirstRun = false;
       }
  
          try
          {
  
              String tempAddr = tbAddr.Text;
              IPAddress ipAd = IPAddress.Parse(&quot;147.197.204.172&quot;);
              // use local m/c IP address, and 
              // use the same in the client
              String tempPort = tbPort.Text;
              /* Initializes the Listener */
              TcpListener myList = new TcpListener(ipAd, 3000);
  
              /* Start Listeneting at the specified port */
              myList.Start();
              tcConnection1 = &quot;Console:\n&quot; + &quot;The server is running at port 3000...&quot;;
              tcConnection2 = &quot;\n&quot; + &quot;The local End point is  :&quot; + myList.LocalEndpoint;
              tcConnection3 = &quot;\n&quot; + &quot;Waiting for a connection.....&quot;;
  
              while (true)
              {
                  s = myList.AcceptSocket();
                  if (s != null)
                  {
                      if (connectionEstab == false)
                      {
                          tcEstab = &quot;\n&quot; + &quot;Connection accepted from &quot; + s.RemoteEndPoint;
                          connectionEstab = true;
                      }
  
  
                      byte[] b = new byte[100];
                      int k = s.Receive(b);
                      //Console.WriteLine(&quot;Recieved...&quot;);
                      for (int i = 0; i &lt; k; i++)
                      {
                          //Console.Write(Convert.ToChar(b[i]));
                          tcValue = tcValue + Convert.ToString(b[i]);
                          valueArray[ii] = (float)Convert.ToDouble(b[i]);
                      }
                      tcValue = tcValue + &quot;\n&quot;;
                      ii++;
                  }
                  else
                  {
                      Thread.Sleep(200);
                  }
              }
  
              ASCIIEncoding asen = new ASCIIEncoding();
              s.Send(asen.GetBytes(&quot;The string was recieved by the server.&quot;));
              rtbConsole.Text = rtbConsole.Text + &quot;\n&quot; + &quot;Sent Acknowledgement&quot;;
              /* clean up */
              s.Close();
              myList.Stop();
          }
          catch (Exception ex)
          {
              tcError = &quot;\n\n&quot; + &quot;Error..... &quot; + ex.StackTrace;
          }
  }
  </code></pre>
  <p>Working method starts the server and blocks upon calling myList.AcceptSocket();?</p>
  <pre><code>private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
  {
          if ((e.Cancelled == true))
          {
              rtbConsole.Text = rtbConsole.Text + &quot;\n&quot; + &quot;Canceled!&quot;;
          }
  
          else if (!(e.Error == null))
          {
              rtbConsole.Text = rtbConsole.Text + &quot;\n\n&quot; + (&quot;Error: &quot; + e.Error.Message);
          }
  
          else
          {
              rtbConsole.Text = rtbConsole.Text + &quot;\n&quot; + &quot;Done!&quot;;
          }
  }
  
  private void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
  {
          rtbConsole.Text = rtbConsole.Text + &quot;\n&quot; + (e.ProgressPercentage.ToString() + &quot;%&quot;);
  }
  
  private void stopButton_Click(object sender, EventArgs e)
  {
          checkTimer.Enabled = false;
          if (bw.WorkerSupportsCancellation == true)
          {
              bw.CancelAsync();
          }
  }
  </code></pre>
  <p>Other methods for some completeness.</p>
  <p>Sending data from my android device is received by a console app running 1 thread, however nothing seems to happen in this windows form application upon sending data to the same ip and port from the same program on the same device. The separate thread just stays blocked and the android app cannot complete communication to send the data.</p>