我已經閱讀了很多關於此主題的文章和其他問題,但是因爲我對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能幫助我,以簡單的方式解釋如何解決這個和背景的工人是如何工作的。 問候
謝謝你的回答。這很容易理解,我現在就試試看。 :) – MansNotHot