2009-01-31 9 views
-3

J'ai un projet CopyFile et Directory. Mais quand j'ai commencé à copier Gui, ça gèle. Je ne peux rien faire avec la copie de fichiers. J'ai donc trouvé ma solution chez BackgroundWorker Component. Mais j'ai aussi un problème avec ce composant.Comment puis-je résoudre ce problème impliquant BackgroundWorkers en C#?

Il y a 3 boutons radio et bouton de commande. Lorsque je clique sur le bouton de commande, il est vérifier si radiobutton1 est cochée ou bien si radiobutton2 vérifié ou bien si radiobutton3 est cochée. Si radiobutton1 est cochée, faites une des choses beaucoup, ou si radiobutton2 cochée, faire autre chose, etc. Il y a 3 backgroundworkers pour 3 radiobuttons. Quand j'ai vérifié radiobutton1, backgroundworker1 dowork événement de travail. Quand j'ai vérifié radiobutton2, backgroundworker2 dowork travail de l'événement. etc

Mon problème est quand j'ai vérifié radiobutton1 et cliquez sur le bouton de commmand. Commencer backgroundworker1 faire un événement de travail mais il continue également à contrôler si radiobutton2 est cochée ou non. Ça ne s'arrête pas, alors j'ai des erreurs. Mon code est ci-dessous:

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

son contrôle si radiobutton1 chechked ou non si ses bgworkers vrais ses start vérifiés font l'événement de travail et sa va continuer si radiobutton2 vérifié ou pas si radiobutton3 vérifié ou non Blabla son pas s'arrêter quand son voir radiobutton1 vérifié vrai.


oui je veux arrêter de contrôler les est vrai que faire d'un autre RadioButton chechked vrai ou not.If radiobutton1 de chechked événement de backgroundworkers et STOP.

Répondre

2

Je ne sais pas exactement quel est exactement votre problème, mais dans votre code si votre radioButton1 est Vérifié alors l'arrière-plan travaillera, sinon il ne le fera pas. N'est-ce pas ce que tu veux?

Je ne vois aucun code pour les autres boutons radio que vous mentionnez.


Ah, vous voulez donc arrêter les travailleurs d'arrière-plan lorsque vous vérifiez un autre bouton radio? Dans votre méthode dowork(), vous devrez vérifier si bg.CancellationPending == true et quitter la méthode si c'est le cas.

Vous devez également définir WorkerSupportsCancellation sur true après l'initialisation du worker BG.

Questions connexes