长时间运行任务的视觉反馈 [英] Visual feedback on long running task
问题描述
我有一个长时间运行的 for-each 循环,想知道是否有一种惯用的方式来添加一些视觉用户反馈,以便用户不会认为应用程序崩溃了.
I have a long running for-each loop, and was wondering if there was a idiomatic way to add some visual user feedback so the user doesn't think the application crashed.
private void btnRunLongRunningTask_Click(object sender, EventArgs e)
{
foreach(string path in Directory.EnumerateFiles(@"path"), "*.ext", SearchOption.AllDirectories))
{
var result = LongRunning.Task(path);
string resultPath = Manipulate(path);
// write result to resultPath
}
}
这可能会有所帮助:任务本身花费的时间并不多,但可能会有很多.
This might help: It isn't so much that the task itself takes long, but there will likely be a lot of them.
关于如何实现这一目标的任何建议?由于我得到了一个目录作为参数,我想我会查找任务将执行的次数,然后相应地更新进度条,在后台工作程序中运行任务和更新代码,注意跨线程访问问题.
Any advice on how I can accomplish this? Since I'm given a directory as argument, I was thinking I'd look up how many times the task will be performed, then update a progressbar accordingly, running the task and the update code in a background worker, watching out for crossthread access issues.
推荐答案
您可以将工作移至 BackgroundWorker 并使用 ReportProgress 方法.
You could move the work to a BackgroundWorker and use the ReportProgress method.
for (i = 0; i < count; i++)
{
// do work
worker.ReportProgress((100 * i) / count);
}
private void MyWorker_ProgressChanged(object sender,
ProgressChangedEventArgs e)
{
taskProgressBar.Value = Math.Min(e.ProgressPercentage, 100);
}
这篇关于长时间运行任务的视觉反馈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!