我正在使用C#中的Windows应用程序。
我有一个表格,并且具有所有方法的类。
我在类中的方法中,我在ArrayList中处理一些文件。 我想调用进度条的方法,此文件处理,但它不工作。
任何帮助
PFB我的代码片段:
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
}
}
}
你要使用的BackgroundWorker组件,其中DoWork
处理程序包含您的实际工作(该string[] allFiles1
部分及以后)。 它会是这个样子:
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();
}