0

I need to enable and disable button based on completion of a thread.

In this case i have only one thread along with the form. i used Thread.isAlive method but it seems not working. Please see the code below.

    Thread t1 = new Thread(() =>
      {
          Thread.CurrentThread.IsBackground = true;

          foreach (DataRow drRow in DtSet.Tables[0].Rows)
          {
              //Using Acess Data base
              try
              {
                  // MessageBox.Show(drRow[2].ToString());
                  if (drRow[2].ToString() == "Terminated Employees" || drRow[2].ToString() == "New Employees" || drRow[2].ToString() == "")
                  {
                      drRow.Delete();
                  }
                  else
                  {
                      str = FindDivisionLocation(drRow[0].ToString());

                      //MessageBox.Show(str);
                      if (str != "" || str != null)
                      {


                          if (str.Contains("Abu Dhabi"))
                          {
                              dept = str.Substring(0, str.IndexOf("Abu Dhabi"));
                              location = "Abu Dhabi";
                          }
                          if (str.Contains("Kuwait"))
                          {
                              dept = str.Substring(0, str.IndexOf("Kuwait"));
                              location = "Kuwait";
                          }
                          if (str.Contains("Qatar"))
                          {
                              dept = str.Substring(0, str.IndexOf("Qatar"));
                              location = "Qatar";
                          }
                          if (str.Contains("AirportJV"))
                          {
                              dept = str.Substring(0, str.IndexOf("AirportJV"));
                              location = "AirportJV";
                          }
                          if (str.Contains("Saudi Arabia"))
                          {
                              dept = str.Substring(0, str.IndexOf("Saudi Arabia"));
                              location = "Saudi Arabia";
                          }
                          if (str.Contains("Oman"))
                          {
                              dept = str.Substring(0, str.IndexOf("Oman"));
                              location = "Oman";
                          }
                          if (str.Contains("Bahrain"))
                          {
                              dept = str.Substring(0, str.IndexOf("Bahrain"));
                              location = "Bahrain";
                          }
                          if (str.Contains("Dubai"))
                          {
                              dept = str.Substring(0, str.IndexOf("Dubai"));
                              location = "Dubai";
                          }

                          drRow[4] = dept;
                          drRow[5] = location;


                      }
                  }
              }
              catch (Exception ex)
              {
                  MessageBox.Show(ex.ToString());
              }

          }
      });
    t1.Start();
    dataGridView1.DataSource = DtSet.Tables[0];

    if(t1 .IsAlive )
    {
        button2.Enabled = false;
    }
    else
    {
        button2.Enabled = true;
    }
}
Rinu
  • 354
  • 2
  • 6
  • 18

0 Answers0