C#进度条 - 不能换我的头周围 [英] C# Progress bar - can't wrap my head around it
问题描述
我有一个程序,通过文件需要循环的文件夹中,与该文件的名称在其他地方创建一个新的文件夹,然后提出一个共同文件文件夹中。直到我们有什么该文件夹中(在列表框列出)进行冲洗重复。
I have a program that takes loops through files in a folder, creates a new folder elsewhere with that file name, then puts a common file in that folder. rinse repeat until we are done with whats in that folder (listed in a listbox).
我决定我要成为看中,并有进度条。我从概念上理解,我是一个单位计数(在 listbox.items.count
)每次迭代..我所编码的和行之有效的。
I decided I want to be fancy and have a progress bar. I conceptually understand that I am increment the count by one (to the listbox.items.count
) for each iteration.. which I have coded and works well.
我也明白了的DoWork
事件是进度条才算数,我怎么拿到两网?难道我传递一个专柜的DoWork
每次迭代?
I also understand that the DoWork
event is what counts for the progress bar, how do I get the two to mesh? Do I pass a counter to the DoWork
for each iteration?
我只是缺少从我的主代码循环到柜台的DoEvents的桥梁。
I am just missing the bridge from my main code loop to the DoEvents counter.
推荐答案
使用一个如图所示的BackgroundWorker:
Use a backgroundworker as shown:
BackgroundWorker worker = new BackgroundWorker();
worker.WorkerReportsProgress = true;
worker.DoWork += new DoWorkEventHandler(update_DoWork);
worker.ProgressChanged += new ProgressChangedEventHandler(update_ProgressChanged);
worker.RunWorkerAsync();
您需要一个ProgressChangedEventHandler。然后你需要,当你想更新进度条(这将是在DoWork的方法)来使用ReportProgress。此方法将调用ProgressChangedEventHandler
You need a ProgressChangedEventHandler. Then you will need to use ReportProgress when you want to update the progress bar (this will be in the DoWork method). This method will call the ProgressChangedEventHandler.
int percentProgress = 10;
worker.ReportProgress(percentProgress)
一ProgressChangedEventHandler的一个例子:
An example of a ProgressChangedEventHandler:
private void update_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar.Value = (e.ProgressPercentage);
}
这篇关于C#进度条 - 不能换我的头周围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!