2014-04-16 24 views
6

İlk kez bir arka plan çalışanı ayarlıyorum. Çoğunlukla kod çalışıyor ve stop/cancel butonum çalışıyor. Bununla birlikte, bir ilerleme çubuğunu güncellemek için ilerlemeyi bildirmeye çalışıyorum ama bunu hiç bir şekilde ateşleyemiyorum.Arkaplan Çalışanı RaporuProblemi tetikleme

Bu kodu çalışan bir düğme tıklama kodunu başlatın:

backgroundWorker1.WorkerSupportsCancellation = true; 
backgroundWorker1.WorkerReportsProgress = true; 
backgroundWorker1.RunWorkerAsync();//this invokes the DoWork event 

Benim Do_Work yöntemi:

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) 
{ 
    string tbProgress = (e.ProgressPercentage.ToString() + "%"); 
    MessageBox.Show(tbProgress + "backgroundWorker1"); 
    importProgressBar(e.ProgressPercentage); 
} 

private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e) 
{ 
    string tbProgress = (e.ProgressPercentage.ToString() + "%"); 
    MessageBox.Show(tbProgress + "worker"); 
    importProgressBar(e.ProgressPercentage); 
} 
: Bu ProgressChanged olaylardan biri ateş etmeye çalışılıyor

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) 
     { 
      BackgroundWorker worker = sender as BackgroundWorker; 

      int j = 0;// Count cumulative imported files 
      int countDupFiles = 0;// Count number of previously imported csv files 
      int countImportedFiles = 0;// Count imported files 


      foreach (string folderPath in csvDirList) 
      { 
       string[] csvFileNames = Directory.GetFiles(@folderPath, "*.csv"); 
       frmImportCsvData.replaceAll(csvFileNames, folderPath + "\\", ""); 

       for (int i = 0; i < csvFileNames.Length; i++, j++) 
       { 
        string csvFilePath = folderPath + "\\" + csvFileNames[i]; 

        if ((worker.CancellationPending == true)) 
        { 
         e.Cancel = true; 
         break; 
        } 
        else 
        { 
         if (dataLayer.ImportCsvDataBkgrnd(this, csvFilePath, compIdValue, csvFileCount, i))//new method processes subdirectories if tick box selected 
         { 
          countImportedFiles = countImportedFiles + 1; 
         } 
         else 
         { 
          countDupFiles = countDupFiles + 1; 
         } 

         System.Threading.Thread.Sleep(500); 

        } 

        worker.ReportProgress(j);//tried using worker and backgroundWorker1 but neither works 
        backgroundWorker1.ReportProgress(j); 

        //string proj = j.ToString(); 
        //MessageBox.Show(proj);//Displays incrementing j as expected when not commented out 
       } 
      } 
      if (countImportedFiles > 0) 
       MessageBox.Show(countImportedFiles + " files were imported."); 
      if (countDupFiles > 0) 
       MessageBox.Show(countDupFiles + " files were not imported. Matches all ready in Database."); 
     } 

Son olarak, ProgressChanged olayının, ilerleme çubuğumu güncellemek için bu yöntemi tetiklemesini istiyorum:

public void importProgressBar(int i) 
{ 
    progressTableLayoutPanel.Visible = true;//display progress bar 

    int percProgress = 100 * (i + 1)/csvFileCount; 

    if (percProgress <= 99)// Required to prevent values above 100 that crash the code 
     progressBar.Value = percProgress + 1;//hack that makes the progress bar update when progress value decreases 
    progressBar.Value = percProgress; 
    percProgressLabel.Text = percProgress.ToString(); 

    progressTableLayoutPanel.Update();//Required to display all progress bar table contents 
    //Thread.Sleep(200); 

    if (percProgress >= 100) 
    { 
     Thread.Sleep(200); 
     progressTableLayoutPanel.Visible = false; 
    } 
} 
, şöyle çalışır düğme kodunu iptal: my ProgressChanged olaylarda

private void stopImportButton_Click(object sender, EventArgs e) 
     { 
      backgroundWorker1.CancelAsync(); 
     } 

messageboxes göstermek asla ve benim ilerleme çubuğu görünür ayarlı asla. Sorunun ne olabileceği hakkında bir fikrin var mı?

cevap

7

Kontrol Bu örnek:

BackgroundWorker bgw = new BackgroundWorker();  
    public Form1() 
    { 
     InitializeComponent(); 
     label1.Text = ""; 
     label2.Text = ""; 
    } 

    private void button1_Click_1(object sender, EventArgs e) 
{ 
    if (bgw == null) 
    { 
     bgw = new BackgroundWorker(); 
     bgw.DoWork += new DoWorkEventHandler(bgw_DoWork); 
     bgw.ProgressChanged += new ProgressChangedEventHandler(bgw_ProgressChanged); 
     bgw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgw_RunWorkerCompleted); 
    } 
    bgw.WorkerReportsProgress = true; 
    bgw.WorkerSupportsCancellation = true; 
    bgw.RunWorkerAsync(); 
} 

    void bgw_DoWork(object sender, DoWorkEventArgs e) 
    { 
     int total = 57; //some number (this is your variable to change)!! 

     for (int i = 0; i <= total; i++) //some number (total) 
     { 
      System.Threading.Thread.Sleep(100); 
      int percents = (i * 100)/total; 
      bgw.ReportProgress(percents, i); 
      //2 arguments: 
      //1. procenteges (from 0 t0 100) - i do a calcumation 
      //2. some current value! 
     } 
    } 

    void bgw_ProgressChanged(object sender, ProgressChangedEventArgs e) 
    { 
     progressBar1.Value = e.ProgressPercentage; 
     label1.Text = String.Format("Progress: {0} %", e.ProgressPercentage); 
     label2.Text = String.Format("Total items transfered: {0}", e.UserState); 
    } 

    void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) 
    { 
     //do the code when bgv completes its work 
    } 
} 

Belki bu sorun size yardımcı olur ...

Ve düğme click olayı size background.doWork diyoruz hemen sonra görünür olarak ilerleme koymak deneyin .

+0

Araç kutusundan backgroundworker bileşenini kullanıyordum. Ancak, örneğinizdeki gibi manuel olarak ayarlamak, hepsini çalışmak zorunda kaldım. –

+1

Teşekkürler :) –

+0

Bir hata buldum. İçe aktarma sürecimi bir kereden fazla çalıştırırsam, ek bir arka plan çalışanı oluşturuldu ve böylece ikinci çalıştırmada, kodun tamamı iki kez çalıştı, üçüncüsü 3 kez çalıştı, vb. Eğer (bgw = null) ifadesi yaratmayı önler birden fazla bgw. –