[英]Single error popup during multiple async call in WPF MVVM application
[英]wpf application exits on first async call
我有一个wpf应用程序,对于大多数用户而言,它都能完美地工作,但是,对于大约10%的用户,当我调用第一个异步函数时,该应用程序在启动后立即退出,这是启动代码的一小段
来自app.xaml.cs:
private static app application;
private view app_view;
public TaskbarIcon sys_tray_icon;
private async Task init_remote_conf(){
// XXX: debug start->exit bug
log.write_debug("before simple_task");
await util.sleep_async(500);
log.write_debug("before get_server_conf_async");
// ........ other async init calls
}
private async void startup_cb(){
util.cleanup_tray_icons();
sys_tray_icon = (TaskbarIcon)FindResource("tray_icon");
sys_tray_icon.Visibility = Visibility.Hidden;
app_view = new view(application);
sys_tray_icon.DataContext = app_view;
conf.get_cmd_args();
sys_tray_icon.Visibility = Visibility.Visible;
log.perr("start", true);
await init_remote_conf();
log.write_debug("after init_remote_conf");
// ......... other startup code
}
protected override void OnExit(ExitEventArgs e){
sys_tray_icon.CloseBalloon();
sys_tray_icon.Dispose();
base.OnExit(e);
}
private void exit_cb(string reason){
log.perr("exit", "reason: "+reason, null, null, true, true);
}
private static bool unhandled_exception_sent = false;
private static void crash_cb(Exception e){
if (unhandled_exception_sent)
return;
log.perr("unhandled_exception", e.Message, e.StackTrace, null,
true, true);
unhandled_exception_sent = true;
// Exit event isn't called if we're in error state
application.exit_cb("unhandled_exception");
}
[STAThread]
public static void Main(string[] args){
if (!SingleInstance<app>.InitializeAsFirstInstance(unique))
return;
AppDomain.CurrentDomain.UnhandledException +=
(s, e) => crash_cb((Exception)e.ExceptionObject);
log.init();
application = new app();
application.ShutdownMode = ShutdownMode.OnExplicitShutdown;
application.InitializeComponent();
application.DispatcherUnhandledException += (s, e) => {
crash_cb(e.Exception); };
application.Exit += (s, e) =>
application.exit_cb(e.ApplicationExitCode.ToString());
application.SessionEnding += (s, e) => {
log.perr("session_ending", e.ReasonSessionEnding.ToString(),
null, null, true, true);
};
application.Startup += (s, e) => application.startup_cb();
application.Run();
// Allow single instance code to perform cleanup operations
SingleInstance<app>.Cleanup();
}
来自util.cs:
public static async Task<bool> sleep_async(int ms){
await Task.Factory.StartNew(() => {
System.Threading.Thread.Sleep(ms); });
return true;
}
现在,对于大多数用户而言,调试打印会生成:
在simple_task之前
在get_server_conf_async之前
init_remote_conf之后
但是对于少数不幸的人,他们只会
在simple_task之前
然后立即调用应用程序exit_cb。
增加了sleep_async只是为了调试行为(在执行任何其他更复杂的异步任务之前)。
我不知道是什么原因导致启动后不久就导致应用程序调用shutdown。
编辑 (更多信息):
有任何想法吗?
我终于弄清楚了为什么会发生这种情况,并希望分享以供将来参考。
当所有wpf应用程序初始化示例都在调用Application.Run之前或之后不久设置了Application MainWindow时,有一个很好的理由。
在startup_cb代码末尾的异步调用之后,我正在设置主窗口。 在某些情况下,在某些计算机上,这导致wpf认为应用程序处于关闭条件并致电Shutdown。
将主窗口设置为start_cb的开头解决了该问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.