Dan*_*Dan 2 c# wpf async-await
我遇到了一个问题,我需要在主窗口打开和显示之前运行异步任务。IE
[STAThread]
static void Main(string[] args)
MainWindow window = new MainWindow();
SplashScreen.Show("Authenticating");
await Authenticate(); // Need something similar to this....
SplashScreen.Close();
new Application().Run(window);
}
static async Task Authenticate()
{
// Load Local Auth Data
var result = await Authenticator.Authenticate(); // Validates google token with webservice
if (result.GoogleReauthRequired)
{
if (MessageBox.Show(......) == MessageBoxResult.Yes)
{
Google.Reauthenticate();// Opens web browser for account to be logged into using OAuth
result = await Authenticator.Authenticate();
}
}
// Initialize state
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,由于启动 Main 函数不是异步的(也不能因为它是 STAThread)。我不能像上图那样简单地做到这一点。
我尝试了其他几种方法,例如:
MainWindow window = new MainWindow();
SplashScreen.Show("Authenticating");
Authenticate().Wait();
SplashScreen.Close();
new Application().Run(window);
Run Code Online (Sandbox Code Playgroud)
但这一个不起作用,因为身份验证代码使用 async/await 发出 Web 请求,因此将自阻塞,因为它永远无法重新进入同步上下文。
因此,要解决这个问题,您最初会认为只需将其添加到任务中即可:
MainWindow window = new MainWindow();
SplashScreen.Show("Authenticating");
Task.Run(async () => await Authenticate()).Wait();
SplashScreen.Close();
new Application().Run(window);
Run Code Online (Sandbox Code Playgroud)
但这也失败了,因为 Authentication 方法可能会打开一个消息框,这要求它位于主线程而不是工作线程上。
我的第三次尝试也出现了问题,我尝试了最小化窗口、从任务栏删除、将可见性设置为隐藏/折叠等的几种变体和组合
MainWindow window = new MainWindow();
// Visibility attempts
window.Loaded += async (a, b) =>
{
await Authentication();
// Undoing the above visibility attempts
SplashScreen.Close();
};
SplashScreen.Show("Authenticating");
new Application().Run(window);
Run Code Online (Sandbox Code Playgroud)
使用这种方法,没有死锁,但是在初始化代码完成之前(即身份验证/验证)窗口被渲染和访问,这也是不可取的。
显然,上面的代码已被简化,足以显示我的问题。
所以我的问题是如何在窗口对客户端可见之前运行这个异步方法。
我知道在身份验证完成之前只在窗口中显示加载屏幕是一个选项,但我的启动画面非常美观,我更愿意使用它。
您可以在OnStartup应用程序的方法(内部App.xaml.cs)中执行这些操作,而不是在 Main 方法中执行这些操作。只需确保OnStartup使用async修饰符标记方法并StartupUri从App.xaml.
示例App.xaml.cs:
namespace WpfApp1
{
public partial class App : Application
{
protected override async void OnStartup(StartupEventArgs e)
{
SplashScreen.Show("Authenticating");
await Authenticate();
SplashScreen.Close();
MainWindow = new MainWindow();
MainWindow.Show();
}
}
}
Run Code Online (Sandbox Code Playgroud)
示例App.xaml:(无StartupUri属性)
<Application x:Class="WpfApp1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>
</Application>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3370 次 |
| 最近记录: |