对于循环运行两次

时间:2009-05-25 18:24:25

标签: c# for-loop

有人可以告诉我为什么这会处理所有文件然后再次执行吗?这让我疯狂。感谢

    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;
    }

2 个答案:

答案 0 :(得分:8)

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

答案 1 :(得分:2)

您确定该功能未被调用两次吗?那个循环看起来很好。