Warm tip: This article is reproduced from serverfault.com, please click

async await-WPF

(async await - WPF)

发布于 2020-12-04 17:57:30

嗨,我正在为程序测试一些东西,每次测试完成时,都会显示一个新的消息框。问题在于,在此之前我关闭消息框后,只会打开一个新的消息框。我尝试将消息框放到新任务中,但是那也不起作用...

Task.Run(() => System.Windows.MessageBox.Show(
                dirs[directory] + " is done, it took " + stopwatch.Elapsed,
                "Done with " + dirs[directory],
                System.Windows.MessageBoxButton.OK,
                System.Windows.MessageBoxImage.Information,
                System.Windows.MessageBoxResult.OK,
                System.Windows.MessageBoxOptions.ServiceNotification));

我还尝试使方法异步并等待Task.Run,​​那里也没有运气

 await Task.Run(() => System.Windows.MessageBox.Show(
                dirs[directory] + " is done, it took " + stopwatch.Elapsed,
                "Done with " + dirs[directory],
                System.Windows.MessageBoxButton.OK,
                System.Windows.MessageBoxImage.Information,
                System.Windows.MessageBoxResult.OK,
                System.Windows.MessageBoxOptions.ServiceNotification));
Questioner
toni
Viewed
1
Paulo Morgado 2020-12-06 01:59:14

MessageBox.Show调用MessageBox函数(winuser.h)

备注中,你可以阅读:

如果在出现对话框时创建消息框,请使用对话框的句柄作为hWnd参数。hWnd参数不应标识子窗口,例如对话框中的控件。

但是你无法获得hWnd消息框的。

你最好为此创建一个自定义窗口。