繁体   English   中英

具有backgroundworker的进度栏

[英]Progressbar with backgroundworker

如果用户选中了该文件夹的复选框,我将使用下面的代码将某些文件夹复制到其他位置。

我有一个背景工作人员和一个进度栏。 我看到这个网站上甚至MSDN上的人都提供了相同的示例来更新进度条,

for (int i = 0; i <= 100; i++)
{
    // Report progress to 'UI' thread
    backgroundWorker1.ReportProgress(i);
    // Simulate long task
    System.Threading.Thread.Sleep(100);
}

一切都很好,我对它的工作原理有所了解。 但是我不知道要实现我的复选框,如果选中了该文件夹,则将其复制,然后根据我有多少个复选框来更新进度栏。 我对复选框进行计数,并将其分配给prgbarmax。

这是我到目前为止的内容:

private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{           
   for (int i = 0; i < prgbarmax; i++)
    {
        int step = (i / prgbarmax) * 100;
        if (test1)
        {
            //do the copy here
            backgroundWorker1.ReportProgress(i);                    
        }

        if (tes2)
        {
            //do the copy here
            backgroundWorker1.ReportProgress(i);
        }

        if (test3)
        {
            //do the copy here                  
            backgroundWorker1.ReportProgress(i);
        }
        .... so on
    }
}

您需要构造要复制的文件夹列表,并且在for循环的每次迭代中仅复制一个文件夹。 代码的问题是您试图一次迭代复制所有文件夹。

为了说明代码示例中的想法。

private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
    //construct the list of folder to be copied
    List<DirectoryInfo> listOfFolders = new List<DirectoryInfo>();
    if (test1) 
       listOfFolders.Add(folder1);
    if (test2)
       listOfFolders.Add(folder2);
    if (test3)
       listOfFolders.Add(folder3);

    //begin to copy
    for (int i = 0; i < listOfFolders.Count; i++)
    {
        listOfFolders[i].Copy(...); //copy only one folder in the list
        int step = ((i + 1) / listOfFolders.Count) * 100; //calculate the progress
        backgroundWorker1.ReportProgress(step);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM