【发布时间】:2009-05-25 18:24:25
【问题描述】:
谁能告诉我为什么这会处理所有文件然后再做一次?这让我疯狂。谢谢
private void HP3BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker hp3worker = (BackgroundWorker) sender;
DirectoryInfo hp3Files = new DirectoryInfo(fromPath + @"\hp3\");
FileInfo[] hp3Filelist = hp3Files.GetFiles("*.*");
int count = hp3Filelist.Length;
UseWaitCursor = true;
for (int i = 0; i < count; i++)
{
FileInfo file = hp3Filelist[i];
try
{
File.Copy(fromPath + @"\hp3\" + file.Name, toPath + @"\hp3\" + file.Name, true);
hp3worker.ReportProgress((int)((float) i / count * 100));
}
catch (Exception error)
{
MessageBox.Show("Error is " + error);
}
}
UseWaitCursor = false;
}
【问题讨论】:
-
需要更多信息。没有明显的理由让它运行两次 - DoWork 函数是否被调用了两次?
-
如果这段代码是从 FileSystemWatcher 事件中调用的,它可能会被调用两次。
-
可能吗?你为什么不先检查一下?如果我们不知道该方法是否被调用了两次,我们应该如何帮助您?
-
它被调用了两次。我跑了很多次。我不知道错过了!谢谢!