title: Reduce C# Application CPU Usage
body: <p>I am creating a program that will TCP Event Receiver. Every time I start the program having is CPU consumption 30% and try to publish it and try to run multiple application  it gradually consume 100% of my CPU Usage</p>
  <pre><code>namespace TCPIPReceiver
  </code></pre>
  <p>{
  public partial class Form1 : Form
  {
  SimpleTcpServer server;
  SqlConnection con;
  BackgroundWorker backgroundworker1;
  Thread INCOMING;</p>
  <pre><code>    bool rxv = false;
      string rxvmsg;
      public Form1()
      {
          InitializeComponent();
          loadit();
      }
      private void loadit()
      {
          server = new SimpleTcpServer();
          server.Delimiter = 0x13;
          server.StringEncoder = Encoding.UTF8;
          server.DataReceived += server_datarx;
          backgroundworker1 = new BackgroundWorker();
          backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
          INCOMING = new Thread(INCOMINGTHREAD);
  
      }
  
      private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
      {
          while (true)
          {
              if(rxv)
              {
                  rxv = false;
                  INCOMING = new Thread(INCOMINGTHREAD);
                  INCOMING.Start();
              }
              if (backgroundWorker1.CancellationPending)
              {
                  e.Cancel = true;
                  return;
              }
          }
      }
  
      private void server_datarx(object sender, SimpleTCP.Message e)
      {
          rxvmsg = e.MessageString;
          rxv = true;
          SetText(rxvmsg);
          e.ReplyLine(&quot;\u0006&quot;);
      }
  
      delegate void SetTextCallback(string text);
      private void SetText(string text)
      {
          if (this.textBox2.InvokeRequired)
          {
              SetTextCallback d = new SetTextCallback(SetText);
              this.Invoke(d, new object[] { text });
          }
          else
          {
              if (text != null)
              {
                  this.textBox2.AppendText(Environment.NewLine);
                  this.textBox2.AppendText(text);
              }
          }
      }
      private void button1_Click(object sender, EventArgs e)
      {
          if((textBox1.Text == &quot;&quot;) || (textBox3.Text == &quot;&quot;))
          {
              MessageBox.Show(&quot;All fields are required&quot;,&quot;System Notification&quot;,MessageBoxButtons.OK,MessageBoxIcon.Information);
          }
          else
          {
              Cursor.Current = Cursors.WaitCursor;
              startit();
              Cursor.Current = Cursors.Default;
  
          }
      }
  
      private void startit()
      {
          if (button1.Text == &quot;Start&quot;)
          {
              con = new SqlConnection(textBox3.Text);
              if(connectionchecker())
              {
                  backgroundWorker1.RunWorkerAsync();
                  server.Start(Convert.ToInt32(textBox1.Text));
                  button1.Text = &quot;Stop&quot;;
                  SetText(&quot;Established Database Connection&quot;);
                  settingslocker(true);
                  
  
              }
              else
              {
                  MessageBox.Show(&quot;Please check database connection&quot;, &quot;Database connection error&quot;, MessageBoxButtons.OK, MessageBoxIcon.Information);
              }
          }
          else if (button1.Text == &quot;Stop&quot;)
          {
              server.Stop();
              button1.Text = &quot;Start&quot;;
              settingslocker(false);
              if (backgroundWorker1.IsBusy)
              {
                  backgroundWorker1.CancelAsync();
              }
          }
      }
  
      private void settingslocker(bool indi)
      {
          if(indi)
          {
              textBox1.Enabled = false;
              textBox3.Enabled = false;
          }
          else
          {
              textBox1.Enabled = true;
              textBox3.Enabled = true;
          }
      }
  
      private bool connectionchecker()
      {
          try
          {
              con.Open();
              con.Close();
              return true;
          } catch
          {
              return false;
          }
      }
  </code></pre>
  <p>My question is how do I reduce CPU usage running multiple same application I build?</p>