title: c# restart a tcp server
body: <p>I have the following c# form application which is a tcp server which handles incoming file request. I would like to restart the thread when there is some exception handling or via manual button press. The problem is that once i invoke the thread Abort() function, the whole application freezes. </p>
  
  <p>I do understand there are other methods in doing this more gracefully. How do I do then? Thanks!</p>
  
  <pre><code>  public partial class Form1 : Form
  {
      Thread t2;
  
      public Form1()
      {
          InitializeComponent();
      }
  
      private void Form1_Load(object sender, EventArgs e)
      {
           t2 = new Thread(delegate()
          {
              startServer();
          });
          t2.Start();
  
      }
  
      private void restartServer()
      {
          t2.Abort();
          t2.Start();
      }
  
      private void startServer()
      {
  
              // Listen on port 1234      
          TcpListener tcpListener = new TcpListener(IPAddress.Any, 1234);
  
              tcpListener.Start();
  
              Console.WriteLine("Server started");
              textBox1.Invoke((MethodInvoker)(() =&gt; textBox1.Text += "server started"));
              //Infinite loop to connect to new clients      
              while (true)
              {
                  // Accept a TcpClient      
                  TcpClient tcpClient = tcpListener.AcceptTcpClient();
  
                  //textBox1.Text += "Connected to client\n";
                  textBox1.Invoke((MethodInvoker)(() =&gt; textBox1.Text += "Connected to client"));
                  StreamReader reader = new StreamReader(tcpClient.GetStream());
  
                  // The first message from the client is the file size      
                  string cmdFileSize = reader.ReadLine();
  
                  // The first message from the client is the filename      
                  string cmdFileName = reader.ReadLine();
  
                  int length = Convert.ToInt32(cmdFileSize);
                  byte[] buffer = new byte[length];
                  int received = 0;
                  int read = 0;
                  int size = 1024;
                  int remaining = 0;
  
                  // Read bytes from the client using the length sent from the client      
                  while (received &lt; length)
                  {
                      remaining = length - received;
                      if (remaining &lt; size)
                      {
                          size = remaining;
                      }
  
                      read = tcpClient.GetStream().Read(buffer, received, size);
                      received += read;
                  }
  
                  // Save the file using the filename sent by the client      
                  String fileName = Path.GetFileName(cmdFileName);
                  String path1 = Path.GetDirectoryName(Application.ExecutablePath) + "\\ReceivedLogs\\" + fileName;
                  Console.WriteLine("path:"+path1);
                  using (FileStream fStream = new FileStream(path1, FileMode.Create))
                  {
                      fStream.Write(buffer, 0, buffer.Length);
                      fStream.Flush();
                      fStream.Close();
                  }
                  textBox1.Invoke((MethodInvoker)(() =&gt; textBox1.Text += "File received and saved in " + Environment.CurrentDirectory + "\n"));
                 // textBox1.Text += ("File received and saved in " + Environment.CurrentDirectory+"\n");
              }
          }
  
      private void label1_Click(object sender, EventArgs e)
      {
  
      }
  
      private void textBox1_TextChanged(object sender, EventArgs e)
      {
  
      }
  
      private void button1_Click(object sender, EventArgs e)
      {
          restartServer();
      }
  
  
      }
  }
  </code></pre>
  
  <p><strong>Edit 1</strong>:Changed all invoke to 'BeginInvoke' and the freeze still persist.</p>