我的应用程序启动后需要大约3秒钟执行一个动作。在UI线程上实现延迟动作的最佳方式
internal static class Entry
{
private static SplashScreen splashScreen;
[STAThread]
internal static void Main()
{
ShowSplashScreen();
StartApp();
}
private static void ShowSplashScreen()
{
splashScreen = new SplashScreen("Splash.png");
splashScreen.Show(false, true);
}
private static void StartApp()
{
var app = new App();
//this, in particular, is ugly and more difficult to comprehend than I'd like
var dispatcherTimer = new DispatcherTimer();
dispatcherTimer.Interval = TimeSpan.FromSeconds(3);
dispatcherTimer.Tick += delegate
{
CloseSplashScreen();
dispatcherTimer.Stop();
};
dispatcherTimer.Start();
app.Run();
}
private static void CloseSplashScreen()
{
splashScreen.Close(TimeSpan.FromSeconds(1));
}
}
我找到StartApp()
代码比较难看,但一直没能炮制了一个更简洁的选择:我如下付诸实施。我在这里错过了一个常见的成语吗?
PS。是的,我知道SplashScreen
有一个自动关闭选项。我不想使用它,主要是因为它在应用程序加载后立即关闭,这是我不想做的。