[英]Running a background task and waiting for its result without blocking the UI thread
I want to create a Splash Screen on my WinForms application.我想在我的 WinForms 应用程序上创建一个启动画面。 I create the splash screen form and run the initialization operation.
我创建启动画面表单并运行初始化操作。
public async Task RunApplication()
{
splash = new SplashWindow();
splash.Show();
await Task.Run(InitializeAsync);
Application.Run(new frmMain());
}
//the InitializeAsync function
private async Task InitializeAsync()
{
splash.Status = "Test";
}
//the status property
public string Status
{
get { return status; }
set {
if (status != value)
{
status = value;
lblStatus.InvokeIfRequired(() => lblStatus.Text = value);
}
}
}
public static void InvokeIfRequired(this ISynchronizeInvoke snc,
MethodInvoker action)
{
if (snc.InvokeRequired) {
snc.Invoke(action, null);
} else {
action();
}
}
As you can see, I want to be able to change the label to the current status.如您所见,我希望能够将 label 更改为当前状态。 However, when I run this code, as soon as I hit the
snc.Invoke(action, null);
但是,当我运行此代码时,只要我点击
snc.Invoke(action, null);
line, the program hangs.行,程序挂起。 I did some research/debugging and it seems that when I do
Task.Run
, it will block the UI thread and since the splash has been created on that thread, the program is never able to run that action on the UI thread.我做了一些研究/调试,似乎当我执行
Task.Run
时,它会阻塞 UI 线程,并且由于在该线程上创建了启动画面,因此程序永远无法在 UI 线程上运行该操作。
What I ended up doing was to wait until the task is finished and constantly do the application events.我最终做的是等到任务完成并不断地做应用程序事件。
var task = Task.Run(InitializeAsync);
while(!task.IsCompleted)
{
Application.DoEvents();
Thread.Sleep(10);
}
Applicaiton.Run(new frmMain());
This works.这行得通。 However, I was wondering if there's a more elegant solution for this.
但是,我想知道是否有更优雅的解决方案。
First, get rid of InvokeIfRequired
.首先,摆脱
InvokeIfRequired
。 InvokeRequired
- regardless of how common it is - is a serious code smell. InvokeRequired
不管它有多常见——是一种严重的代码异味。
Instead, you can get rid of Task.Run
and just call InitializeAsync
directly:相反,您可以摆脱
Task.Run
并直接调用InitializeAsync
:
public async Task RunApplication()
{
splash = new SplashWindow();
splash.Show();
await InitializeAsync();
Applicaiton.Run(new frmMain());
}
private async Task InitializeAsync()
{
splash.Status = "Test";
}
public string Status
{
get => lblStatus.Text;
set => lblStatus.Text = value;
}
If you do need to use Task.Run
for some reason (ie, if InitializeAsync
has CPU-bound or blocking work to do), then you can use Progress<T>
:如果您出于某种原因确实需要使用
Task.Run
(即,如果InitializeAsync
有 CPU-bound 或阻塞工作要做),那么您可以使用Progress<T>
:
public async Task RunApplication()
{
splash = new SplashWindow();
splash.Show();
var progress = new Progress<string>(update => splash.Status = update);
await Task.Run(() => InitializeAsync(progress));
Applicaiton.Run(new frmMain());
}
private async Task InitializeAsync(IProgress<string>? progress)
{
progress?.Report("Test");
}
public string Status
{
get => lblStatus.Text;
set => lblStatus.Text = value;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.