title: Running a TCPListener while my program opens a form
body: <p>My program starts a TCPListener when I click a button.
  Before it opens the TCPListener, it opens a new window.
  I want the program to construct the items in the window while the TCPListener is running, aka I need the TCPListener to run in 'the background'.
  I'm thinking I need to use threading but I'm not sure how exactly.</p>
  
  <p>Right now basically:</p>
  
  <ol>
  <li>Opens the form</li>
  <li>Runs the TCPListener forever</li>
  <li>Freezes the form window before it even constructs anything because it is waiting for the TCP Listener to finish. The while loop in the TCP Listener is probably wrong as well. I wish it to keep accepting data.</li>
  </ol>
  
  <p>Here's the code for the method the button runs. The openForm method is also in the same form as the button is. </p>
  
  <pre><code>    private void openForm()
      {
          if (!textBox1.Text.Equals("") &amp;&amp; !textBox2.Text.Equals("") &amp;&amp; !textBox3.Text.Equals("") &amp;&amp; !textBox4.Text.Equals(""))
          {
              string fornavn = textBox1.Text;
              string efternavn = textBox2.Text;
              string medarbejdernr = textBox3.Text;
              string organisation = textBox4.Text;
              Form f = new Form1(fornavn, efternavn, medarbejdernr, organisation, this);
              f.Show();
              this.Visible = false;
              Listener listen = new Listener();
          }
          else
          {
              MessageBox.Show("Du skal angive noget i alle felter", "Fejl", MessageBoxButtons.OK, MessageBoxIcon.Error);
          }
      }
  </code></pre>
  
  <p>And here's my TCP Listener class.</p>
  
  <pre><code>class Listener
  {
      const int PORT_NO = 5000;
      const string SERVER_IP = "127.0.0.1";
      string dataReceived;
  
      public Listener()
      {
          //---listen at the specified IP and port no.---
          IPAddress localAdd = IPAddress.Parse(SERVER_IP);
          TcpListener listener = new TcpListener(localAdd, PORT_NO);
          listener.Start();
          while (true)
          {
              listen(listener);
          }
      }
      private void listen(TcpListener listener)
      {
  
          //---incoming client connected---
          TcpClient client = listener.AcceptTcpClient();
  
          //---get the incoming data through a network stream---
          NetworkStream nwStream = client.GetStream();
          byte[] buffer = new byte[client.ReceiveBufferSize];
  
          //---read incoming stream---
          int bytesRead = nwStream.Read(buffer, 0, client.ReceiveBufferSize);
  
          //---convert the data received into a string---
          dataReceived = Encoding.ASCII.GetString(buffer, 0, bytesRead);
          MessageBox.Show(dataReceived +" har fået et anfald mkay", "Patient har fået anfald mkay", MessageBoxButtons.OK, MessageBoxIcon.Error);
          //---write back the text to the client---
  
          client.Close();
      }
  }
  </code></pre>