【发布时间】:2017-01-31 20:28:51
【问题描述】:
我有一个窗口,里面有一个浏览器控件。当有人关闭窗口时,我需要将浏览器控件导航到特定的 URL,并且只有在浏览器完成导航后才需要关闭窗口。
当窗口关闭时,OnBeforeWindowClose(Eventargs e) 方法被调用。
OnBeforeWindowClose(Eventargs e)
{
// As soon as the URL property is set the browser control navigates to the URL
URL = "new URL";
//I need to wait till the browser is done navigating. Based on the NavigationFinished property below
}
bool NavigationFinished {set; get;}
我不能使用Thread.Sleep,因为它会导致 UI 线程休眠。我想不出一种方法来暂停 OnBeforeClose 的执行,直到 NavigationFinised 变为真。不阻塞 UI 线程。
更新:将以下循环添加到 OnBeforeWindowClose 方法对我有用
do
{
var dateTime = DateTime.Now.AddSeconds(2);
while (DateTime.Now < dateTime)
{
System.Windows.Forms.Application.DoEvents();
}
} while (!NavigationFinished);
但我想知道是否有更好的方法来做到这一点,而不是在 .Net 4.0 中使用循环
【问题讨论】:
-
您正在寻找
await。 -
它在 .net 4.0 中不可用 :(
-
在 NuGet 上安装
Microsoft.Bcl.Async。 -
我无法使用 Microsoft.Bcl.Async。但是将以下循环添加到 OnBeforeWindowClose 方法对我有用。有没有更好的方法来停止方法执行而不是循环。
-
做 { var dateTime = DateTime.Now.AddSeconds(2);而(日期时间。现在