我有下面的代碼將一些文件夾複製到不同的位置,如果用戶有複選框選中該文件夾。背景工作進度條
我有一個backgroundworker和一個progresbar。我看到人們在這個網站,甚至在MSDN上給出了同樣的例子與
for (int i = 0; i <= 100; i++)
{
// Report progress to 'UI' thread
backgroundWorker1.ReportProgress(i);
// Simulate long task
System.Threading.Thread.Sleep(100);
}
這是所有罰款和我得到它是如何工作的理念更新進度。但我想不出的是實現我的複選框,並複製文件夾,如果它被檢查,然後更新進度欄取決於我有多少複選框。我將選中的方塊計數並分配給prgbarmax。
這是我到目前爲止有:
private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
for (int i = 0; i < prgbarmax; i++)
{
int step = (i/prgbarmax) * 100;
if (test1)
{
//do the copy here
backgroundWorker1.ReportProgress(i);
}
if (tes2)
{
//do the copy here
backgroundWorker1.ReportProgress(i);
}
if (test3)
{
//do the copy here
backgroundWorker1.ReportProgress(i);
}
.... so on
}
}
您使用的是WPF還是Windows Forms? – Transcendent
您需要構建要複製的文件夾列表,並且在for循環的每次迭代中僅複製一個文件夾。你的代碼的問題是你試圖在一次迭代中複製所有的文件夾。 – kennyzx
@kennyzx我對C#非常陌生,並且正在努力學習我的代碼。你能給我一個代碼示例嗎? – Besiktas