title: Stop Thread on Button Click in C#
body: <p>I want to stop my thread on button click event and As I am new to threads I don't know how can I do this in C#. My application is of TCP Client and I am reading TCP server continuously using this thread </p>
  
  <pre><code>public void ClientReceive()
  {
      try
      {
  
          stream = client.GetStream(); //Gets The Stream of The Connection
          new Thread(() =&gt; // Thread (like Timer)
          //Thread mythread = new Thread(ClientReceive);
          {
              //MessageBox.Show(stream.Read(datalength, 0, 256).ToString());
              //(i = stream.Read(datalength, 0, 256)) != 0
              while (i != 1)//Keeps Trying to Receive the Size of the Message or Data
              {
                  // how to make a byte E.X byte[] examlpe = new byte[the size of the byte here] , i used BitConverter.ToInt32(datalength,0) cuz i received the length of the data in byte called datalength :D
                  // byte[] data = BitConverter.GetBytes(1000); // Creates a Byte for the data to be Received On
                  byte[] data = new byte[1000];
                  stream.Read(data, 0, data.Length); //Receives The Real Data not the Size
                  this.Invoke((MethodInvoker)delegate // To Write the Received data
                  {
                      //txtLog.Text += System.Environment.NewLine + "Server : " + Encoding.Default.GetString(data); // Encoding.Default.GetString(data); Converts Bytes Received to String
                      DateTime now = DateTime.Now;
                      //MessageBox.Show(Encoding.Default.GetString(data));
                      if (Encoding.Default.GetString(data) != "")
                      {
                          txtLog.Text += System.Environment.NewLine + now.ToString() + " Received : \r\n" + Encoding.Default.GetString(data) + "\r\n";
  
                      }
                      for (int j = 0; j &lt; txtLog.Lines.Length; j++)
                      {
                          if (txtLog.Lines[j].Contains("Received"))
                          {
                              this.CheckKeyword(txtLog.Lines[j + 1], Color.Red, 0);
                          }
                          if (txtLog.Lines[j].Contains("Sent"))
                          {
                              this.CheckKeyword(txtLog.Lines[j + 1], Color.Blue, 0);
                          }
                      }
  
                  });
              }
         // mythread.Start();
          }).Start(); // Start the Thread
      }
      catch (Exception ex)
      {
          MessageBox.Show(ex.ToString());  
      }
  }
  </code></pre>