我不知道要在哪里运行我的代码以执行操作并使用进度栏对其进行跟踪。我想循环通过一个字符串[]列表,然后对每行进行处理,然后将“进度”映射到进度条。
我发布了代码,也许有人知道如何解决此问题?
我试图将代码放在不同的区域,但是没有任何作用
public Form1()
{
InitializeComponent();
worker = new BackgroundWorker();
worker.WorkerReportsProgress = true;
worker.WorkerSupportsCancellation = true;
worker.DoWork += new DoWorkEventHandler(worker_DoWork);
worker.ProgressChanged +=
new ProgressChangedEventHandler(worker_ProgressChanged);
worker.RunWorkerCompleted +=
new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
}
BackgroundWorker worker;
int precentagemax;
int precentagenow;
void worker_DoWork(object sender, DoWorkEventArgs e)
{
int percentFinished = (int)e.Argument;
if (!worker.CancellationPending && percentFinished < 100)
{
String[] lines = rtxtB_Systems.Text.Split('\n');
lines = lines.Where(x => !string.IsNullOrEmpty(x)).ToArray();
foreach (var line in lines)
{
System.Threading.Thread.Sleep(5000);
percentFinished++;
worker.ReportProgress(percentFinished);
}
}
e.Result = percentFinished;
}
void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
pBar.Value = e.ProgressPercentage;
}
void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
MessageBox.Show("Task finished untill: " +e.Result.ToString() +@"%");
}
我希望程序在字符串[]列表中运行并在GUI上显示进度