Beispiel #1
0
        private static async Task <ErrorCode> StartAppAsync(string[] args, RunOptions options)
        {
            SetDirectories(options);

            try
            {
                using (SyncContext = GlobalContext.Create())
                {
                    if (SyncContext.IsFirst)
                    {
                        // First instance, start like normal

                        InitLogger(options);
                        await InitProgramAsync();

                        SyncContext.BeginListen();

                        try
                        {
                            var code = (ErrorCode)BuildAvaloniaApp().StartWithClassicDesktopLifetime(args, ShutdownMode.OnMainWindowClose);
                            if (code != ErrorCode.NoError)
                            {
                                Log.Warning("Application returned error code '{0}'", code);
                            }
                            else
                            {
                                Log.Debug("Application exited gracefully");
                            }
                            return(code);
                        }
                        catch (Exception ex)
                        {
                            Log.Error(ex, "Application crashed");
                            return(ErrorCode.General);
                        }
                        finally
                        {
                            SyncContext.EndListen();
                            await UnloadProgramAsync();
                        }
                    }
                    else
                    {
                        // App already running, pass arguments and exit
                        try
                        {
                            var message = Parser.Default.FormatCommandLine(options);
                            await SyncContext.SendMessageAsync(message);

                            return(ErrorCode.NoError);
                        }
                        catch
                        {
                            return(ErrorCode.General);
                        }
                    }
                }
            }
            finally
            {
                SyncContext = null;
            }
        }