我正在將大文件從一處複製到另一處。 這需要很長時間,所以我決定使用進度條。使用進度條複製項目
我跟隨此example。
copyItems()
函數遍歷列表項並從其他位置複製項目。它又調用一個複製一個項目的函數CopyListItem。
我需要將backgroundWorker1.ReportProgress(i)綁定到項目的總數no,即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();
}
}
}
有什麼問題? – stefankmitph 2015-03-03 10:10:03
你是說你有問題報告progress_或者_「我不想使用thread.sleep()」_ – MickyD 2015-03-03 10:18:31
你需要顯示一個時間而不是進度我猜,像是估計的時間... – 2015-03-03 10:19:45