2015-03-03 84 views
1

我正在將大文件從一處複製到另一處。 這需要很長時間,所以我決定使用進度條。使用進度條複製項目

我跟隨此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(); 
     } 
    } 
} 
+2

有什麼問題? – stefankmitph 2015-03-03 10:10:03

+0

你是說你有問題報告progress_或者_「我不想使用thread.sleep()」_ – MickyD 2015-03-03 10:18:31

+0

你需要顯示一個時間而不是進度我猜,像是估計的時間... – 2015-03-03 10:19:45

回答

0

你需要做的複製行爲在BackgroundWorker的DoWork的-事件。所以當你打電話給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處理程序比你必須得到來自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處理程序只遞增ProgressBar的價值:

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

如果你想擁有在ProgressChanged處理程序更多的信息可以傳遞作爲UserState的附加對象。這可以通過object additional = e.UserState;

右鍵打電話之前backgroundWorker1.RunWorkerAsync(new WorkerItem(...))你應該progressBar1的最大設定量SPListItems像得到:

progressBar1.Maximum = itemscoll.Count; 

BackgroundWorker只報告它的進度你,如果你設置。

backgroundWorker1.WorkerReportsProgress = true; 

如果您希望在BackgroundWorker完成後你可以得到RunWorkerCompleted-事件予以通報。

backgroundWorker1.RunWorkerCompleted += BackgroundWorker1OnRunWorkerCompleted;