2017-10-09 48 views
0

我已經閱讀了很多關於此主題的文章和其他問題,但是因爲我對C#非常陌生,並且在一般情況下使用線程工作,所以答案很可悲,主要是對於我來說很複雜,或者不適用於我的問題。在另一個線程中更新進度條

我有這個進度條,其中將會填充大部分0.03%的非常小的步驟。當通常包括這個時,它會在操作的最後階段從0-100開始,儘管我每次都會運行並每次以0.03%的步長更新它。我相信在另一個線程中更新進度條會起到訣竅的作用,但是我很遺憾現在還沒有對這個話題有所瞭解。

這是代碼部分,在那裏我的進度條進行更新:

public void CopyAll(DirectoryInfo source, DirectoryInfo target) 
{ 
    string source4count = source.ToString(); 
    if (checkSubdirCase == 0) 
    { 
     allfiles = System.IO.Directory.GetFiles(source4count, "*.*", System.IO.SearchOption.AllDirectories); 
     allFilesNum = allfiles.Length; 
     progressbarinterval = 0;      
     progressbarinterval = 100/allFilesNum; 
     Progressbarvalue = 0; 
    } 

    if (Directory.Exists(target.FullName) == false) 
    { 
     Directory.CreateDirectory(target.FullName); 
    } 

    foreach (FileInfo fi in source.GetFiles()) 
    { 
     //HERE IS THE UPDATING OF THE PROGRESSBAR 
     fi.CopyTo(System.IO.Path.Combine(target.ToString(), fi.Name), true); 
     Progressbarvalue = Progressbarvalue + progressbarinterval;      
     ProgressBarCopy.Value = Progressbarvalue; 
    } 
    foreach (DirectoryInfo diSourceSubDir in source.GetDirectories()) 
    { 
     DirectoryInfo nextTargetSubDir = target.CreateSubdirectory(diSourceSubDir.Name); 
     checkSubdirCase = 1; 
     CopyAll(diSourceSubDir, nextTargetSubDir); 
    } 
} 

我希望someeone能幫助我,以簡單的方式解釋如何解決這個和背景的工人是如何工作的。 問候

回答

2

你可能會想創建一個後臺工作(這發生在UI線程)是這樣的:

var worker = new BackgroundWorker(); 
worker.WorkerReportsProgress = true; 
worker.DoWork += new DoWorkEventHandler(WorkerDoWork); 
worker.ProgressChanged += new ProgressChangedEventHandler(WorkerProgressChanged); 

在你的DoWork法,你會做,你現在做的CopyAll一切方法,只有進度處理會改變。你需要調用worker.ReportProgress(...),然後這會再回到UI線程並調用你的WorkerProgressChanged方法。在WorkerProgressChanged-method中,你可以更新你的進度條。

但是,你應該知道的事實,即UI線程和你的BackgroundWorker的線程之間的切換是相當昂貴的,所以當它真的通過對UI的效果的量發生變化,你只能報告進度。

+2

謝謝你的回答。這很容易理解,我現在就試試看。 :) – MansNotHot

2

一個其他的解決辦法是在進度

變化您的按鈕

//CopyAll(); //Old 
Task.Run(() => CopyAll()); 

你的方法與調用

public void CopyAll(DirectoryInfo source, DirectoryInfo target) 
{ 
    string source4count = source.ToString(); 
    if (checkSubdirCase == 0) 
    { 
     allfiles = System.IO.Directory.GetFiles(source4count, "*.*", System.IO.SearchOption.AllDirectories); 
     allFilesNum = allfiles.Length; 
     progressbarinterval = 0;      
     progressbarinterval = 100/allFilesNum; 
     Progressbarvalue = 0; 
    } 

    if (Directory.Exists(target.FullName) == false) 
    { 
     Directory.CreateDirectory(target.FullName); 
    } 

    foreach (FileInfo fi in source.GetFiles()) 
    { 
     //HERE IS THE UPDATING OF THE PROGRESSBAR 
     fi.CopyTo(System.IO.Path.Combine(target.ToString(), fi.Name), true); 
     Progressbarvalue = Progressbarvalue + progressbarinterval;      
     ProgressBarCopy.Invoke(new MethodInvoker(delegate { ProgressBarCopy.Value = Progressbarvalue; })); 
    } 
    foreach (DirectoryInfo diSourceSubDir in source.GetDirectories()) 
    { 
     DirectoryInfo nextTargetSubDir = target.CreateSubdirectory(diSourceSubDir.Name); 
     checkSubdirCase = 1; 
     CopyAll(diSourceSubDir, nextTargetSubDir); 
    } 
} 
+0

我與Task.run(() - > CopyAll();應該放在哪裏,它是做什麼?UPATE:得到它。我的devEx progressbar似乎沒有「調用」,但我會改變到標準進度條並看到。 – MansNotHot

+0

仍然收到錯誤消息:進度條不包含調用的定義。我不知道是什麼問題.. – MansNotHot

+0

更新:發現。作品。謝謝 – MansNotHot

1

使用的代碼中使用的任務與調用背景工人是一個很好的選擇。

var worker = new BackgroundWorker(); 
worker.DoWork += new DoWorkEventHandler(Worker_DoWork); 
worker.ProgressChanged += new 
ProgressChangedEventHandler(Worker_ProgressChanged); 
worker.RunWorkerCompleted += new 
RunWorkerCompletedEventHandler(Worker_RunWorkerCompleted); 


private void Worker_DoWork(object sender, DoWorkEventArgs e) 
{ 

... 
    foreach (FileInfo fi in source.GetFiles()) 
    { 
    //Copying the files.. 

    // Calling the ReportProgress method would fire the worker_ProgressChanged event 
    worker.ReportProgress(0, progressState) 
} 


    } 

    private void worker_ProgressChanged(object sender, 
             ProgressChangedEventArgs e) 
    { 
     // This is where you would have the UI related changes. 
     //In your case updating the progressbar. 
     // While the files are being copied this would update the UI. 
    } 

private void Worker_RunWorkerCompleted(object sender, 
RunWorkerCompletedEventArgs e) 
{ 
worker.CancelAsync(); 

}