【问题标题】:Copy items using progress bar使用进度条复制项目
【发布时间】:2015-03-03 09:59:12
【问题描述】:

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

我正在关注这个example

copyItems() 函数遍历列表项并从另一个位置复制这些项。它又调用一个函数 CopyListItem 来复制一个项目。

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

我不想使用thread.sleep()

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

只有在复制一个文件时,进度条才需要进行。 IT 需要完成所有文件都复制完毕

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

【问题讨论】:

  • 您是在说 您在报告进度时遇到问题 还是 “我不想使用 thread.sleep()”
  • 我猜你需要显示时间而不是进度,比如估计时间......
  • thread.sleep 函数将每个进度项的时间延迟一个特定的时间延迟。我不知道 copyListItem 函数需要多长时间才能完成。进度条未链接到要复制的项目数。
  • 我会改写前两句话。 “我正在将大文件从一个地方复制到另一个地方。这需要很长时间,所以我决定使用进度条”-> 暗示使用进度条会缩短复制时间。
  • 如果我猜对了:您想在每次复制项目时增加进度条,进度条的最大值应该是项目的总数,对吧?

标签: c# winforms


【解决方案1】:

您需要在 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 的最大值设置为SPListItems 的数量,例如:

progressBar1.Maximum = itemscoll.Count;

如果您设置,您的BackgroundWorker 只会向您报告进度。

backgroundWorker1.WorkerReportsProgress = true;

如果您想在BackgroundWorker 完成时收到通知,您可以获取 RunWorkerCompleted-Event。

backgroundWorker1.RunWorkerCompleted += BackgroundWorker1OnRunWorkerCompleted;

【讨论】:

    猜你喜欢
    • 2022-10-24
    • 2018-12-23
    • 2012-12-23
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    • 1970-01-01
    • 2015-12-12
    相关资源
    最近更新 更多