如何显示Windows应用程序的进度条? [英] How to show progress bar in windows application?
本文介绍了如何显示Windows应用程序的进度条?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用C#在Windows应用程序的工作。
我有一个表格,并且具有所有方法的类。
我在类中的方法,其中我在ArrayList中处理一些文件。我想调用该文件处理进度条的方法,但它不工作。
任何帮助
PFB我代码片段:
公共无效TraverseSource()
{
的String [] = allFiles1 Directory.GetFiles( SOURCEPATH,*的.xml,SearchOption.AllDirectories);
变种allFiles =新的ArrayList();
VAR长度= allFiles.Count;
的foreach(在allFiles1字符串项)
{
如果(item.Substring(item.Length - 6).Equals(MD.xml))
{
allFiles.Add(项目);
//这里我要调用进度条是在形式上
}
}
}
解决方案
您将要使用的的BackgroundWorker 组件,其中的DoWork
处理程序包含您的实际工作(在的String [] allFiles1
部分及以后)。它会是这个样子:
公共无效TraverseSource()
{
//创建BackgroundWorker的
变种工人=新的BackgroundWorker
{
WorkerReportsProgress = TRUE
};
//指派代表到DoWork的事件,这是当'RunWorkerAsync`被称为提高。这是你的实际工作中应该做的
worker.DoWork + =(发件人,参数)=> {
的String [] = allFiles1 Directory.GetFiles(SOURCEPATH,*的.xml,SearchOption.AllDirectories);
变种allFiles =新的ArrayList();
的foreach(VAR I = 0; I< allFiles1.Length;我++)!
{
如果(item.Substring(item.Length - 6).Equals( MD.xml))
{
allFiles.Add(项目);
//通知工人这一进展已经改变了
worker.ReportProgress(I / allFiles.Length * 100);
}
}
};
//分配时`ReportProgress`被调用时引发的委托。此委托调用原始的线程上,这样你就可以安全地更新一个WinForms控制
worker.ProgressChanged + =(发件人,参数)=> {
progressBar1.Value = args.ProgressPercentage;
};
// OK,现在居然开始做工作,
worker.RunWorkerAsync();
}
I am working on a windows application using c#.
I have a form and a class having all methods .
I have a method in class in which i am processing some files in arraylist. I want to invoke progress bar method for this file processing but its not working.
Any help
PFB my code snippet:
public void TraverseSource()
{
string[] allFiles1 = Directory.GetFiles(sourcePath, "*.xml", SearchOption.AllDirectories);
var allFiles = new ArrayList();
var length = allFiles.Count;
foreach (string item in allFiles1)
{
if (!item.Substring(item.Length - 6).Equals("MD.xml"))
{
allFiles.Add(item);
// Here i want to invoke progress bar which is in form
}
}
}
解决方案
You'll want to use a BackgroundWorker component, in which the DoWork
handler contains your actual work (the string[] allFiles1
part and beyond). It'll look something like this:
public void TraverseSource()
{
// create the BackgroundWorker
var worker = new BackgroundWorker
{
WorkerReportsProgress = true
};
// assign a delegate to the DoWork event, which is raised when `RunWorkerAsync` is called. this is where your actual work should be done
worker.DoWork += (sender, args) => {
string[] allFiles1 = Directory.GetFiles(sourcePath, "*.xml", SearchOption.AllDirectories);
var allFiles = new ArrayList();
foreach (var i = 0; i < allFiles1.Length; i++)
{
if (!item.Substring(item.Length - 6).Equals("MD.xml"))
{
allFiles.Add(item);
// notifies the worker that progress has changed
worker.ReportProgress(i/allFiles.Length*100);
}
}
};
// assign a delegate that is raised when `ReportProgress` is called. this delegate is invoked on the original thread, so you can safely update a WinForms control
worker.ProgressChanged += (sender, args) => {
progressBar1.Value = args.ProgressPercentage;
};
// OK, now actually start doing work
worker.RunWorkerAsync();
}
这篇关于如何显示Windows应用程序的进度条?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文