Example #1
0
        protected override void OnExit(ExitEventArgs e)
        {
            base.OnExit(e);

            //
            // Save databases
            //
            AppointmentDatabase.OnSaveCompletedEvent -= AppointmentDatabase_OnSaveCompletedEvent;
            AppointmentDatabase.Save();
            ContactDatabase.OnSaveCompletedEvent -= AppointmentDatabase_OnSaveCompletedEvent;
            ContactDatabase.Save();
            TaskDatabase.OnSaveCompletedEvent -= TaskDatabase_OnSaveCompletedEvent;
            TaskDatabase.Save();
            NoteDatabase.OnSaveCompletedEvent -= NoteDatabase_OnSaveCompletedEvent;
            NoteDatabase.Save();
            SyncDatabase.OnSaveCompletedEvent -= SyncDatabase_OnSaveCompletedEvent;
            SyncDatabase.Save();

            //
            // Unregister system hooks
            //
            SystemEvents.UserPreferenceChanged -= SystemEvents_UserPreferenceChanged;
            SystemEvents.TimeChanged           -= SystemEvents_TimeChanged;

            //
            // Delete autorecover information
            //
            new RecoveryDatabase(RecoveryVersion.Current).Delete();
            new RecoveryDatabase(RecoveryVersion.LastRun).Delete();

            //
            // Install any updates which may have been downloaded.
            //
            string localPath = (new FileInfo(Process.GetCurrentProcess().MainModule.FileName)).DirectoryName;

            Process.Start(localPath + "\\UpdateManager.exe", "/update " + Process.GetCurrentProcess().Id.ToString());
        }
Example #2
0
        private async void Application_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
        {
            // We don't want to overwhelm the user with multiple error dialogs. However,
            // we will still log the error for debugging purposes.
            if (_isErrorDialogShowing)
            {
                e.Handled = true;

                await Task.Factory.StartNew(() => { Log(e.Exception); });

                return;
            }

            _isErrorDialogShowing = true;

            try
            {
                // We handle our own errors, so that we can write error logs
                // and attempt application recovery.
                e.Handled = true;

                if (e.Exception is NotImplementedException)
                {
                    TaskDialog td = new TaskDialog(MainWindow, "Not Implemented",
                                                   !string.IsNullOrEmpty(e.Exception.Message) ? e.Exception.Message
                                                : "This isn't hooked up yet. Check for updates and try again.", MessageType.Error);
                    td.ShowDialog();

                    _isErrorDialogShowing = false;

                    return;
                }

                await Task.Factory.StartNew(() => { Log(e.Exception); });

                // Try and save the databases, but we still place this in a catch
                // in case an error in the database caused the crash.
                bool saveError = false;

                await Task.Factory.StartNew(() => { try { AppointmentDatabase.Save(); } catch { saveError = true; } });

                if (saveError)
                {
                    saveError = false;
                    await Task.Factory.StartNew(() => { try { ContactDatabase.Save(); } catch { saveError = true; } });
                }

                if (saveError)
                {
                    saveError = false;
                    await Task.Factory.StartNew(() => { try { TaskDatabase.Save(); } catch { saveError = true; } });
                }

                if (saveError)
                {
                    saveError = false;

                    try { ((MainWindow)MainWindow).SaveNotesView(); }
                    catch { }

                    await Task.Factory.StartNew(() => { try { NoteDatabase.Save(); } catch { saveError = true; } });
                }

                if (saveError)
                {
                    saveError = false;
                    await Task.Factory.StartNew(() => { try { SyncDatabase.Save(); } catch { saveError = true; } });
                }

                if (saveError)
                {
                    saveError = false;
                    await Task.Factory.StartNew(() => { try { QuoteDatabase.Save(); } catch { saveError = true; } });
                }

                if (saveError)
                {
                    try
                    {
                        // Autorecover
                        RecoveryDatabase recovery = new RecoveryDatabase(RecoveryVersion.LastRun);

                        MainWindow window = (MainWindow)MainWindow;

                        try { recovery.RecoveryAppointment = window.LiveAppointment; }
                        catch { }
                        try { recovery.RecoveryContact = window.LiveContact; }
                        catch { }
                        try { recovery.RecoveryTask = window.LiveTask; }
                        catch { }
                    }
                    catch { }
                }

                try { new RecoveryDatabase(RecoveryVersion.Current).Delete(); }
                catch { }

                if (MainWindow != null)
                {
                    try { ((MainWindow)MainWindow).SaveSettings(); }
                    catch { }
                }

                // Try and show an error message, but again we still place this in a catch
                // in case an error in the dialog caused the crash.
                try
                {
                    Error error = new Error(null, e.Exception);
                    if (error.ShowDialog() == true)
                    {
                        Feedback f = new Feedback(FeedbackType.Error, e.Exception.ToString());

                        if (MainWindow != null && MainWindow.IsLoaded)
                        {
                            f.Owner = MainWindow;
                        }
                        else
                        {
                            f.WindowStartupLocation = WindowStartupLocation.CenterScreen;
                        }

                        f.ShowDialog();
                    }
                    else if (error.IgnoreError)
                    {
                        _isErrorDialogShowing = false;
                        return;
                    }
                }
                catch
                {
                    if (Settings.JoinedCEIP)
                    {
                        MessageBox.Show("Oops...\r\n\r\n"
                                        + e.Exception.ToString(),
                                        GlobalAssemblyInfo.AssemblyName + " Error",
                                        MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                    else
                    {
                        MessageBox.Show("Oops...\r\n\r\n"
                                        + e.Exception.Message,
                                        GlobalAssemblyInfo.AssemblyName + " Error",
                                        MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                }

                // Restart Dimension 4
                ProcessStartInfo restartInfo = new ProcessStartInfo(Process.GetCurrentProcess().MainModule.FileName, "/nosingleinstance");
                restartInfo.UseShellExecute = true;
                Process.Start(restartInfo);

#if DEBUG
                Debug.WriteLine(e.Exception.ToString());
#endif

                Shutdown(-1);
            }
            catch { }
        }