title: C# application doesn't close
body: <p>I'm having a trouble with my application that is based on various forms and various threads. The threads are closing before the application ends, but even so I can't terminate my application with 100% accuracy. </p>
  
  <p>Sometimes the process still continues to work but nothing is showing up (<code>Form</code>s or even <code>Thread</code>s). I think the problem is on program.cs, almost sure it is, so I'll paste here the code</p>
  
  <pre><code> private static Process old;
      /// &lt;summary&gt;
      /// The main entry point for the application.
      /// &lt;/summary&gt;
      [STAThread]
      static void Main()
      {
          XmlConfigurator.Configure();
          if (PriorProcess() != null)
          {
              try
              {
                  old.Kill();
              }
              catch (Exception ex)
              {
                  MessageBox.Show("Ja existe uma instancia do SpotLight em execucao.", "Aviso");
                  return;
              }                
          }
  
              Application.EnableVisualStyles();
              Application.SetCompatibleTextRenderingDefault(false);
              Application.Run(new Support());
  
      }
  
  
  
      public static Process PriorProcess()
      {
          Process curr = Process.GetCurrentProcess();
          Process[] procs = Process.GetProcessesByName(curr.ProcessName);
          foreach (Process p in procs)
          {
              if ((p.Id != curr.Id) &amp;&amp; (p.MainModule.FileName == curr.MainModule.FileName))
              {
                  old = p;
                  return p;
              }
          }
          return null;
      }
  </code></pre>
  
  <p><code>New Support()</code> its my first form showing up in the application.</p>
  
  <p>This is the code for the threads:</p>
  
  <pre><code>private void check_calls()
  {
          while (stop==false)
      {
          string actualTime = DateTime.Now.ToString("T");
          connection = new MySqlConnection(ConnectionString);
          MySql.Data.MySqlClient.MySqlCommand msqlCommand = new MySql.Data.MySqlClient.MySqlCommand();
          msqlCommand.Connection = connection;
          msqlCommand.CommandText = "Select cd.uniqueid,sup.timespan from contactCenterDevel.cc_cdr_support_pending sup, vboxZon.cdr cd WHERE sup.phone=cd.src AND cd.finish=0 AND cd.accountcode='serviin' AND cd.duration &gt;0 AND cd.lastapp='Vxml' ORDER BY cd.calldate DESC LIMIT 1";
  
          try
          {
              connection.Open();
              MySql.Data.MySqlClient.MySqlDataReader msqlReader = msqlCommand.ExecuteReader();
              while (msqlReader.Read())
              {
                  int timespanCompare = convertTimetoSecs(msqlReader.GetString(1));
                  int actualtimeCompare = convertTimetoSecs(actualTime);
  
                  if (timespanCompare &lt; actualtimeCompare)
                  {
                      updateFlagCDR(msqlReader.GetString(0));
                  }
              }
          }
          catch (Exception er)
          { MessageBox.Show("Mysql actions error: " + er); }
          finally
          { connection.Close(); }
  
         Thread.Sleep(10000);
      }   
  }
  </code></pre>
  
  <p>This one stops when the flag "stop" is changed to true on my logout function. I already tried commentating "thread.sleep" but the problem still appear.</p>