【问题标题】:Backgroundworker with recursive DirectorCopy function具有递归 DirectorCopy 函数的后台工作者
【发布时间】:2013-09-20 14:09:01
【问题描述】:

我在下面花了将近三天的时间,所以我终于到了这里。

我有一个功能(来自 MSDN),它复制一个文件夹,其中包含每个文件和子文件夹。首先它复制主文件夹的文件,然后在每个子文件夹上调用自身。

这里是:

private void DirectoryCopy(string sourceDirName, string destDirName)
    {
        // Get the subdirectories for the specified directory.
        DirectoryInfo dir = new DirectoryInfo(sourceDirName);
        DirectoryInfo[] dirs = dir.GetDirectories();

        if (!dir.Exists)
        {
            throw new DirectoryNotFoundException(
                "Source directory does not exist or could not be found: "
                + sourceDirName);
        }

        // If the destination directory doesn't exist, create it. 
        if (!Directory.Exists(destDirName))
        {
            Directory.CreateDirectory(destDirName);
        }

        // Get the files in the directory and copy them to the new location.
        FileInfo[] files = dir.GetFiles();
        foreach (FileInfo file in files)
        {
            string temppath = System.IO.Path.Combine(destDirName, file.Name);
            file.CopyTo(temppath, false);
        }

        // Copying subdirectories and their contents to new location. 
        foreach (DirectoryInfo subdir in dirs)
        {
            string temppath = System.IO.Path.Combine(destDirName, subdir.Name);
            DirectoryCopy(subdir.FullName, temppath, copySubDirs);
        }
    }

问题是它可能需要很长时间,因此我尝试使用 BackgroundWorker,但我不知道如何将它放在它的 DoWork 事件中。

如果我将第一个 DirectoryCopy 调用放在 DoWork 事件中,则无法处理 Cancel 事件:

private void worker_DoWork(object sender, DoWorkEventArgs e)
    {
        if (worker.CancellationPending)
        {
            e.Cancel = true;
            return;
        }
        DirectoryCopy(sourcePath, destPath, true);
    }

sourcePath 和 destPath 是我班级的成员。

任何提示如何在 DirectoryCopy 中处理工作人员的 Cancel 事件? 或者有什么其他技巧可以让它发挥作用?

谢谢!

【问题讨论】:

  • 尝试在 DirectoryCopy 方法中添加类似 Func cancel 的参数并使用 () => worker.CancellationPending 调用并在循环内检查
  • 尝试将 worker 和 e 都传递给 DirectoryCopy

标签: c# .net recursion backgroundworker


【解决方案1】:

虽然我还没有与 BackgroundWorker 合作过,但看看你的问题 - 一个快速(可能不合逻辑)的建议是在 DirectoryCopy 方法中传递 DoWorkEventArgs e 喜欢

DirectoryCopy (sourcePath, destPath, true, worker , e)

在哪里 BackgroundWoker 工人,DoWorkEventArgs e 并以任何你想要的方式处理它。

例子:

if (worker.CancellationPending)
     {
        // your code
        e.Cancel = true;
     }

希望这会有所帮助!

【讨论】:

  • 它没有用,但正如@Blam 所说,如果我同时通过DoWorkEventArgs eBackgroundWorker worker,它会很好用!
  • @AdamMezovari :谢谢,根据您的反馈编辑了我的答案。
【解决方案2】:

这里最好和最简单的方法是使用Directory.EnumerateDirectories(,,)

这会从您的代码中删除递归并使其更容易停止。

foreach(var dir in Directory.EnumerateDirectories(sourceDir, "*.*", SearchOption.AllDirectories)
{    
   foreach(var file in Directory.EnumerateDirectories(dir, "*.*")
   {
     if (worker.CancellationPending)
     {
        e.Cancel = true;
        return;
     }

     // copy file
   }      
}

【讨论】:

  • 谢谢!我没有提到的唯一问题是它也必须在 Windows 2000 上运行,因此我使用 .NET 2.0,正如我所见,Directory.EnumerateDirectories() 仅支持 4.0。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-29
  • 1970-01-01
  • 2012-08-23
  • 2011-10-30
  • 2011-03-17
相关资源
最近更新 更多