检查线程是否完成

时间:2017-07-09 12:17:01

标签: c# multithreading winforms

我需要根据线程的完成启用和禁用按钮。

在这种情况下,我只有一个线程和表单。我使用Thread.isAlive方法,但它似乎无法正常工作。请参阅下面的代码。

    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;
    }
}

0 个答案:

没有答案