How to wait until BackgroundWorker finish all calls

前端 未结 4 626
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-26 10:30

I have Add button click event how add file:

private void btnAddfiles_Click(object sender, EventArgs e)
{
    if (openFileDialog1.ShowDialog() == Sys         


        
4条回答
  •  礼貌的吻别
    2021-01-26 11:04

    You may use this approch with only one BackgroundWorker.

    BackgroundWorker backgroundWorker;
    
    private void btnAddfiles_Click(object sender, EventArgs e)
    {
        if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            backgroundWorker = new BackgroundWorker();
            backgroundWorker.WorkerReportsProgress = true;
            backgroundWorker.DoWork +=
            (s3, e3) =>
            {
                StartBackgroundFileChecker(openFileDialog1.FileNames);   
            };
    
            backgroundWorker.ProgressChanged +=
            (s3, e3) =>
            {
                //example:
                this.progressBar1.Value = e.ProgressPercentage;
            };
    
            backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(
            (s3, e3) =>
            {
                ///End Here!!
            });
    
            backgroundWorker.RunWorkerAsync();
        }
    }
    private void StartBackgroundFileChecker(string[] files)
    {
        for (int i = 0; i < files.Length; i++)
        {
            string file = files[i];
            System.IO.Stream stream;
            try
            {
                if ((stream = openFileDialog1.OpenFile()) != null)
                {
                    using (stream)
                    {
                        ListboxFile listboxFile = new ListboxFile();
                        listboxFile.OnFileAddEvent += listboxFile_OnFileAddEvent;
                        //Other things...
                    }
                }
            }
    
            catch (Exception ex)
            {
                MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
            }
            backgroundWorker.ReportProgress((i+1) * 100.0/files.Length, file);
        }
    }
    

提交回复
热议问题