在backgroundworker中使用递归方法 [英] use recursive method in backgroundworker

查看:63
本文介绍了在backgroundworker中使用递归方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在里面使用递归方法处理backgroundworker(Cancel,Progressbar,Completed,...)?



how to handle backgroundworker(Cancel, Progressbar, Completed, ...) when use recursive method inside it?

private static void Copyy(string s,string d)
{
    //Exist folder of destination?
    if (!Directory.Exists(d))
        Directory.CreateDirectory(d);

    //Get info folder of destination
    var dir = new DirectoryInfo(s);
        //1
        FileInfo[] files = dir.GetFiles("*.*", SearchOption.TopDirectoryOnly);
    foreach (var file in files)
    {
        File.Copy(file.FullName, Path.Combine(d, Path.GetFileName(file.FullName)), true);

    }
        //2
        DirectoryInfo[] folders = dir.GetDirectories();
        if (folders.Count() != 0)
            foreach (var folder in folders)
                Copyy(folder.FullName, Path.Combine(d, folder.FullName.Replace(s, "").TrimStart('\\')));
}







private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
  {
      try
      {
          BeginInvoke((MethodInvoker)delegate
          {
              lblMessage.Text = String.Empty;
              btnUpdate.Enabled = false;
              But_Cancel.Enabled = true;

              var dir = new DirectoryInfo((string)e.Argument);
              FileInfo[] files = dir.GetFiles("*.*", SearchOption.TopDirectoryOnly);
              progressBar.Minimum = 0;
              progressBar.Maximum = files.Length;
          });

          Copyy((string)e.Argument, @"c:\Test");//<<---------------***

          BeginInvoke((MethodInvoker)delegate
          {
              btnUpdate.Enabled = true;
              But_Cancel.Enabled = false;
              this.lblMessage.Text = @"Done!";
          });
      }
      catch (Exception ex)
      {
          return;
      }
  }

推荐答案

你提到的三件事(取消,进度栏,已完成)完全不同,并且需要一个不同的方法。



完成是一个简单的方法:它在DoWork事件处理程序退出时自动生成。所以你无需处理这个问题,因为所有后台处理都会在那时结束。



取消并不复杂,只需要一些工作:你需要一个类级别bool,它在But_Cancel Click事件处理程序中设置为true,并在Copyy方法的顶部进行检查。如果是真的,立即返回。



ProgressBar更复杂 - 您需要访问BackgroundWorker实例才能通过ReportProgress方法发出ProgressChanged事件的信号。幸运的是,它作为 sender 参数传递给DoWork事件处理程序,因此您需要做的就是将其转换为BackgroundWorker并将其作为参数传递给Copyy方法:

The three things you mentioned (Cancel, Progressbar, Completed) are completely different, and need a differenet approach.

Completed is the easy one: it's automatically generated when the DoWork event handler exits. So there is nothing you need to do to handle this, as all background processing ends at that point.

Cancel is not complex, it just takes a little work: you need a class level bool which is set to true in the But_Cancel Click event handler and checked at the top of your Copyy method. If it's true, return immediately.

A ProgressBar is more complex - you need to have access to the BackgroundWorker instance in order to signal the ProgressChanged event via it's ReportProgress method. Fortunately, it is passed to the DoWork event handler as the sender parameter, so all you need to do is cast that to a BackgroundWorker and pass it into your Copyy method as a parameter:
private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
    {
    BackgroundWorker worker = sender as BackgroundWorker;
    if (worker != null)
        {




private static void Copyy(string s,string d, BackgroundWorker worker = null)
    {

然后,当您想要报告Copyy中的进度时:

Then, when you want to report progress in Copyy:

if (worker != null)
    {
    worker.ReportProgress(percentage);
    }

并在调用每个递归实例时传递worker值。





但是......你确实意识到你根本不需要递归?您可以使用Directory.GetFiles返回文件夹中所有文件的完整路径,包括子目录作为一行,从而无需递归处理文件夹。

And pass the worker value on down when you call each recursive instance.


But...you do realize that you don;t need to be recursive at all? You can use Directory.GetFiles to return the full path to all files in a folder, including subdirectories as one line, eliminating the need to recursively process the folders.


这篇关于在backgroundworker中使用递归方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆