Beispiel #1
0
 /// <summary>
 /// Saves cached port numbers to the file placed at <see cref="CacheFilePath"/>.
 /// </summary>
 /// <returns></returns>
 public static async Task Save()
 {
     await Task.Run(() =>
     {
         try
         {
             var dir = Path.GetDirectoryName(CacheFilePath);
             if (!Directory.Exists(dir)) // Prepare folder
             {
                 Directory.CreateDirectory(dir);
             }
             using (var sr = new StreamWriter(CacheFilePath, false, Encoding.UTF8))
             {
                 new JsonSerializer()
                 {
                     Formatting = Formatting.Indented
                 }
                 .Serialize(sr, RecordDictionary);
             }
         }
         catch (Exception ex)
         {
             MyLogger.Log($"Failed to save {nameof(PortNumberCache)}.", ex);
         }
     }).ConfigureAwait(false);
 }
Beispiel #2
0
 /// <summary>
 /// Loads cached port numbers from the file placed at <see cref="CacheFilePath"/>.
 /// </summary>
 /// <returns></returns>
 public static async Task Load()
 {
     if (File.Exists(CacheFilePath))
     {
         await Task.Run(() =>
         {
             try
             {
                 using (var sr = new StreamReader(CacheFilePath, Encoding.UTF8))
                 {
                     RecordDictionary =
                         new JsonSerializer()
                         .Deserialize(sr, typeof(Dictionary <string, List <int> >))
                         as Dictionary <string, List <int> >;
                 }
             }
             catch (Exception ex)
             {
                 MyLogger.Log($"Failed to load {nameof(PortNumberCache)}.", ex);
             }
         }).ConfigureAwait(false);
     }
     // Initialize empty dictionary
     RecordDictionary = RecordDictionary ?? new Dictionary <string, List <int> >();
 }
Beispiel #3
0
        /// <summary>
        /// Processes an unhandled exception occurred in the UI thread.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            var exception = e.Exception as Exception;

            MyLogger.Log("Unhandled exception.", exception);
            if (ConfirmUnhandledException(exception, "UI Thread"))
            {
                e.Handled = true;
            }
            else
            {
                SingleAppInstanceHelper.Exit();
                Environment.Exit(1);
            }
        }
Beispiel #4
0
        /// <summary>
        /// Processes an unhandled exception occurred in a background task.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
        {
            var exception = e.Exception.InnerException as Exception;

            MyLogger.Log("Unhandled exception.", exception);
            if (ConfirmUnhandledException(exception, "Background Task"))
            {
                e.SetObserved();
            }
            else
            {
                SingleAppInstanceHelper.Exit();
                Environment.Exit(1);
            }
        }
Beispiel #5
0
        protected override void OnStartup(StartupEventArgs e)
        {
            ArgumentsHelper.SetArgs(e.Args);
            base.OnStartup(e);

            // Local method to enqueue connections specified with command-line argument
            bool EnqueueOpenRequestedConnections(IProcessCommander processCommander)
            {
                if (!ArgumentsHelper.HasConnectionSpecified)
                {
                    return(false);
                }
                foreach (var conn in ArgumentsHelper.SpecifiedConnections)
                {
                    MyLogger.Log($"Connection (Type: {conn.Type}, Id: {conn.ConnectionId}) has been enqueued.");
                    processCommander.Invoke(conn.Type, conn.ConnectionId);
                }
                return(true);
            }

            if (SingleAppInstanceHelper.TryStart())
            {
                // Boot as an IPC host
                var service = new ProcessCommander
                {
                    ConnectionRequest = ConnectionRequest.Singleton,
                };
                var serviceHost = IPCService.OpenServiceHost(service);
                EnqueueOpenRequestedConnections(service);
            }
            else
            {
                // Boot as an IPC client
                var channel = IPCService.CreateServiceChannel();
                if (!EnqueueOpenRequestedConnections(channel))
                {
                    MyLogger.Log("Shutting down because another application instance has already run...");
                }
                channel.Activate();
                // Shutdown after activate the primary window
                Shutdown();
            }
        }
Beispiel #6
0
 public App()
 {
     InitializeUnhandledExceptionHandlers();
     Database.SetInitializer(new MigrateDatabaseToLatestVersion <AppDbContext, kenzauros.RHarbor.Migrations.Configuration>());
     MyLogger.Log("Application starting...");
 }