【问题标题】:For Loop runs thru twiceFor Loop 运行两次
【发布时间】: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 事件中调用的,它可能会被调用两次。
  • 可能吗?你为什么不先检查一下?如果我们不知道该方法是否被调用了两次,我们应该如何帮助您?
  • 它被调用了两次。我跑了很多次。我不知道错过了!谢谢!

标签: c# for-loop


【解决方案1】:

检查您是否将事件处理程序HP3BackgroundWorker_DoWork 附加了两次?

【讨论】:

  • 它被调用了两次。我跑了很多次。我不知道错过了!谢谢!
【解决方案2】:

您确定该函数没有被调用两次吗?那个循环看起来不错。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-29
    • 2021-11-21
    • 2016-10-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多