【发布时间】: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 函数需要多长时间才能完成。进度条未链接到要复制的项目数。
-
我会改写前两句话。 “我正在将大文件从一个地方复制到另一个地方。这需要很长时间,所以我决定使用进度条”-> 暗示使用进度条会缩短复制时间。
-
如果我猜对了:您想在每次复制项目时增加进度条,进度条的最大值应该是项目的总数,对吧?