使用进度条复制项目

时间:2015-03-03 09:59:12

标签: c# winforms

我正在将大文件从一个地方复制到另一个地方。 这需要很长时间,所以我决定使用进度条。

我正在关注此example

copyItems()函数遍历列表项并从其他位置复制项。它又调用一个复制一个项目的函数CopyListItem。

我需要将backgroundWorker1.ReportProgress(i)绑定到项目总数,即itemcoll。

我不想使用thread.sleep()

进度条需要显示将文件从一个地方复制到另一个地方所需的实际时间。

复制一个文件时,进度条需要仅在时进度。 复制完所有文件后,需要完成

using System.ComponentModel;
using System.Threading;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, System.EventArgs e)
        {
            // Start the BackgroundWorker.
            backgroundWorker1.RunWorkerAsync();
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            for (int i = 1; i <= itemscoll.count; i++)
            {
                // Wait 100 milliseconds.
                Thread.Sleep(100);
                // Report progress.
                backgroundWorker1.ReportProgress(i);
            }
        }
        private void CopyListItem(SPListItem sourceItem, string destinationListName, string destServerURL)
        {
            // copy items
        }
        private void copyitems()
        {
            try
            {
                int createdYear = 0;
                backgroundWorker1.RunWorkerAsync();
                foreach (SPListItem sourceItem in itemscoll)
                {
                    if (Helper.year == createdYear)
                    {
                        CopyListItem(sourceItem, Helper.destinationListName,Helper.destServerURL);
                        DeleteItem(CompRefNo);
                    }
                }
            }
            catch()
            {}
        }
        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            // Change the value of the ProgressBar to the BackgroundWorker progress.
            progressBar1.Value = e.ProgressPercentage;
            // Set the text.
            this.Text = e.ProgressPercentage.ToString();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您需要在BackgroundWorker的DoWork-Event中执行复制操作。因此,当您调用backgroundWorker1.RunWorkerAsync()时,您必须将包含所有必需信息的对象传递给它。这可能是这样的:

internal class WorkerItem
{
    public WorkerItem(List<SPListItem> spListItems, string destinationListName, string destinationServerURL)
    {
        SPListItems = new List<SPListItem>(spListItems);
        DestinationListName = destinationListName;
        DestinationServerURL = destinationServerURL;
    }

    public List<SPListItem> SPListItems { get; private set; }
    public string DestinationListName { get; private set; }
    public string DestinationServerURL { get; private set; }
}

RunWorkerAsync的调用类似于:

backgroundWorker1.RunWorkerAsync(new WorkerItem(...));

在DoWork-Handler中,您必须从e.Argument获取此对象并将其强制转换为WorkerItem。你可以像以下一样使用它:

private void BackgroundWorker1OnDoWork(object sender, DoWorkEventArgs e)
{
    WorkerItem workerItem = (WorkerItem)e.Argument;

    for (int i = 0; i < workerItem.SPListItems.Count(); i++)
    {
        // CopyListItem is doing the copy for one item.
        CopyListItem(workerItem.SPListItems[i], workerItem.DestinationListName, workerItem.DestinationServerURL);
        ((BackgroundWorker)sender).ReportProgress(i + 1);
    }
}

ProgressChanged-Handler仅增加ProgressBar

的值
private void BackgroundWorker1OnProgressChanged(object sender, ProgressChangedEventArgs e)
{
    progressBar1.Value = e.ProgressPercentage;
}

如果要在ProgressChanged-Handler中包含更多信息,可以将其他对象作为UserState传递。这可以通过object additional = e.UserState;

获得

在你致电backgroundWorker1.RunWorkerAsync(new WorkerItem(...))之前,你应该将progressBar1的最大值设置为SPListItem的数量,如:

progressBar1.Maximum = itemscoll.Count;

如果您设置,BackgroundWorker仅向您报告进度。

backgroundWorker1.WorkerReportsProgress = true;

如果您希望在BackgroundWorker完成后收到通知,您可以获得RunWorkerCompleted-Event。

backgroundWorker1.RunWorkerCompleted += BackgroundWorker1OnRunWorkerCompleted;