/// <summary> /// The constructor is private because <c>IdentityManager</c> /// implements the singleton pattern. To get an instance, use /// <c>IdentityManager.Instance</c> instead. /// </summary> private IdentityManager() { _db = new SQRLDBContext(); _currentIdentityDB = GetIdentityInternal(GetUserData().LastLoadedIdentity); if (_currentIdentityDB != null) { _currentIdentity = DeserializeIdentity(_currentIdentityDB.DataBytes); } _log.Information("IdentityManager initialized."); }
/// <summary> /// Called from various methods to load DB and initialize defaults. /// </summary> public void Initialize() { _db = SQRLDBContext.Instance; _currentIdentityDB = GetIdentityInternal(_appSettings.LastLoadedIdentity); if (_currentIdentityDB != null) { _currentIdentity = DeserializeIdentity(_currentIdentityDB.DataBytes); } else { _currentIdentity = null; } }
// Initialization code. Don't use any Avalonia, third-party APIs or any // SynchronizationContext-reliant code before AppMain is called: things aren't initialized // yet and stuff might break. public static void Main(string[] args) { const string mutexId = @"Global\{{83cfa3fa-72bd-4903-9b9d-ba90f7f6ba7f}}"; Thread ipcThread = new Thread(StartIPCServer); // Set up logging string currentDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); string logFilePath = Path.Combine(currentDir, "log.txt"); Log.Logger = new LoggerConfiguration() .WriteTo.Console() .WriteTo.File(logFilePath, rollingInterval: RollingInterval.Day) .CreateLogger(); Log.Information("New app instance is being launched on {OSDescription}", RuntimeInformation.OSDescription); // Try to detect an existing instance of our app using (var mutex = new Mutex(false, mutexId, out bool created)) { bool hasHandle = false; try { try { hasHandle = mutex.WaitOne(500, false); if (!hasHandle) { // Existing instance detected, forward the first // command line argument if present. Log.Information("Existing app instance detected, forwarding data and shutting down"); ForwardToExistingInstance(args.Length > 0 ? args[0] : IPCServer.MAGIC_WAKEUP_STR); Environment.Exit(1); } } catch (AbandonedMutexException) { hasHandle = true; } // Adds event to handle abrupt program exits and mitigate CPS AppDomain.CurrentDomain.ProcessExit += new EventHandler(CurrentDomain_ProcessExit); // Perform database migrations SQRLDBContext _db = new SQRLDBContext(); _db.Database.Migrate(); // No existing instance of the app running, // so start the IPC server and run the app ipcThread.Start(); BuildAvaloniaApp().StartWithClassicDesktopLifetime(args, Avalonia.Controls.ShutdownMode.OnExplicitShutdown); } finally { Log.Information("App shutting down"); if (hasHandle) { mutex.ReleaseMutex(); } HandleAbruptCPS(); //Remove the notify icon (App.Current as App).NotifyIcon?.Remove(); if (ipcThread.IsAlive) { // Force close the app without waiting // for any threads to finish. Log.Information("Forcing exit because of IPC thread still running."); Environment.Exit(1); } } } }
/// <summary> /// The constructor is private, use the <Instance>property</Instance> /// to get the singletion class instance. /// </summary> private AppSettings() { _db = new SQRLDBContext(); _userData = GetUserData(); Reload(); }
/// <summary> /// Called from various methods to load the DB and pull in default values. /// </summary> public void Initialize() { _db = SQRLDBContext.Instance; _userData = GetUserData(); Reload(); }