2009-01-31 40 views
-3

我有一个CopyFile和Directory项目。但是,当我开始复制桂,它冻结。我无法做任何文件复制。所以我在BackgroundWorker组件找到了我的解决方案。但是我也有这个组件的问题。如何解决涉及C#中BackgroundWorkers的这个问题?

有3个单选按钮和命令按钮。当我单击命令按钮时,它将检查radiobutton1是否被选中,或者如果选中radiobutton2或者如果选中radiobutton3。如果radiobutton1被选中,则执行很多操作,或者如果选中radiobutton2,则执行另一项操作等。有3个背景工作者用于3个单选按钮。当我检查radiobutton1backgroundworker1 dowork事件工作。当我检查radiobutton2backgroundworker2 dowork事件工作。 etc ...

我的问题是当我检查radiobutton1并单击命令按钮。开始backgroundworker1做工作事件,但它也继续控制是否radiobutton2被选中。它并没有停止,所以我得到了错误。我的代码如下:

private void button1_Click(object sender, EventArgs e) 
{ 
    if (radioButton1.Checked) 
    { 
     backgroundWorker1.RunWorkerAsync(); 
    } 

    if (radioButton2.Checked) 
    { 
     StreamReader sr = new StreamReader(Application.StartupPath + @"\hakimler.txt"); 

     while ((satir = sr.ReadLine()) != null) 
     { 
      try 
      { 
       bool copy = CopyDirectory(DosyaYolu.kaynak, @"\\" + satir + @"" + DosyaYolu.hedef, true); 
       if (copy) 
       { 
        kopya += 1; 
       } 
       else 
       { 
        sw.WriteLine(satir); 
       } 
      } 
      catch (Exception) 
      { 

      } 
     } 

     sw.Close(); 
     MessageBox.Show("İşlem tamamlandı", "İşlem Sonu", MessageBoxButtons.OK, MessageBoxIcon.Information); 
     lblkopya.Text = "Başarıyla tamamlanan iş sayısı : " + kopya.ToString(); 
     return; 
    } 

    if (chkPersonel.Checked == true) 
    { 
     StreamReader sr = new StreamReader(Application.StartupPath + @"\personel.txt"); 

     while ((satir = sr.ReadLine()) != null) 
     { 
      try 
      { 
       bool copy = CopyDirectory(DosyaYolu.kaynak, @"\\ab04500-" + satir + @"" + DosyaYolu.hedef, true); 
       //bool copy = CopyDirectory(Application.StartupPath + @"\TELEFON REHBERİ", @"\\" + satir + @"\c$\Documents and Settings\All Users\Start Menu", true); 
       if (copy) 
       { 
        kopya += 1; 
       } 
       else 
       { 
        sw.WriteLine(satir); 
       } 
      } 
      catch (Exception) 
      { 

      } 
     } 

     sw.Close(); 

     MessageBox.Show("İşlem tamamlandı", "İşlem Sonu", MessageBoxButtons.OK, MessageBoxIcon.Information); 
     lblkopya.Text = "Başarıyla tamamlanan iş sayısı : " + kopya.ToString(); 
     return; 
    } 
    else 
    { 
     if (txtBilgisayar.Text == "" && txtDongu.Text == "") 
     { 
      MessageBox.Show("Boşlukları dolduralım bi zahmet :@", "Bilgisayar Kodlarını girelim lütfen!", MessageBoxButtons.OK, MessageBoxIcon.Warning); 
      return; 
     } 

     bilgisayar = Convert.ToInt32(txtBilgisayar.Text); 
     dongu = Convert.ToInt32(txtDongu.Text); 

     for (int i = bilgisayar; i <= dongu; i++) 
     { 
      try 
      { 
       bool copy = CopyDirectory(DosyaYolu.kaynak, @"\\ab04500-" + bilgisayar + @"" + DosyaYolu.hedef, true); 
       if (copy) 
       { 
        kopya += 1; 
       } 
       else 
       { 
       sw.WriteLine(satir); 
       } 
      } 
      catch (Exception) 
      { 

      } 

      if (i == dongu) 
      { 
       sw.Close(); 
       MessageBox.Show("İşlem tamamlandı", "İşlem Sonu", MessageBoxButtons.OK, MessageBoxIcon.Information); 
       lblkopya.Text = "Başarıyla tamamlanan iş sayısı : " + kopya.ToString(); 
      } 

      bilgisayar += 1; 
     } 

} 


private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) 
{ 
    sw = new StreamWriter(DateTime.Today.ToShortDateString().ToString() + "_ulasmayanlar.txt", true); 
    StreamReader sr = new StreamReader(Application.StartupPath + @"\savcilar.txt"); 

    while ((satir = sr.ReadLine()) != null) 
    { 
     try 
     { 
      bool copy = CopyDirectory(DosyaYolu.kaynak, @"\\" + satir + @"" + DosyaYolu.hedef, true); 
      //bool copy = CopyDirectory(Application.StartupPath + @"\TELEFON REHBERİ", @"\\" + satir + @"\c$\Documents and Settings\All Users\Start Menu", true); 
      if (copy) 
      { 
       kopya += 1; 
      } 
      else 
      { 
       sw.WriteLine(satir); 
      } 
     } 
     catch (Exception) 
     { 

     } 
    } 
} 

private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) 
{ 
    sw.Close(); 

    MessageBox.Show("İşlem tamamlandı", "İşlem Sonu", MessageBoxButtons.OK, MessageBoxIcon.Information); 
    lblkopya.Text = "Başarıyla tamamlanan iş sayısı : " + kopya.ToString(); 
} 

其控制,如果radiobutton1 chechked与否如果检查属实的开始bgworkers做的工作事件及其要继续,如果radiobutton2检查与否,如果radiobutton3检查或不BLABLA它不是当它看到radiobutton1被检查为真时停止。


是的,我想停下来控制另一个单选按钮的chechked真或not.if radiobutton1的chechked是真的只做backgroundworkers DoWork的事件而停止。

回答

2

我不太清楚你的问题到底是什么,但是在你的代码中,如果你的radioButton1是 选中,那么后台工作人员会做它的事情,否则它不会。 这不是你想要的吗?

我没有看到您提到的其他单选按钮的任何代码。


啊,所以你想停止后台工作人员,当你检查antoher单选按钮? 在你的dowork()方法中,你必须检查是否bg.CancellationPending == true并退出该方法。

您还需要在初始化BG工作人员后将WorkerSupportsCancellation设置为true。