Mil*_*los 1 c# .net-4.0 winforms
我有带有 Marquee 样式 ProgressBar 的 WaitDialog。
这是显示它的正确方法吗?
var wd = new WaitDialog();
Task.Factory.StartNew(() =>
{
LongRunningMethod();
wd.Close();
});
wd.ShowDialog();
Run Code Online (Sandbox Code Playgroud)请推荐一种正确的方法来报告非 Marquee ProgressBar 任务的进度。
我认为您正在寻找的是一种在不锁定 UI 的情况下运行长时间运行的方法并显示进度对话框的方法。您还需要通过从不同线程访问 UI 来注意跨线程问题。
我建议以这种方式对其进行模式化,这将使您的应用程序在任务运行时保持响应:
var wd = new WaitDialog();
wd.Show(); // Show() instead of ShowDialog() to avoid blocking
var task = Task.Factory.StartNew(() => LongRunningMethod());
// use .ContinueWith to avoid blocking
task.ContinueWith(result => wd.Invoke((Action)(() => wd.Close())));
Run Code Online (Sandbox Code Playgroud)
你展示你的进度对话框——它是否有一个选取框并不重要——然后你开始LongRunningMethod自己的任务。使用该.ContinueWith方法在任务完成后关闭对话框并避免阻塞程序的其余部分。