protected override IntPtr HwndSourceHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) { // Single instance: handling the message if (msg == ((App)Application.Current).Message) { Show(); var args = ClArgs.GetClArgsFromFile(); InstanceManager.Instance.CommmandLineArgs = args.ToList(); //MessageBox.Show("Handled HWND message", "Debug", MessageBoxButton.OK, MessageBoxImage.Exclamation); if (!FileSystemUtils.LoadFileFromClArgs()) { MessageBox.Show("Failed to load file: returned false", "Debug", MessageBoxButton.OK, MessageBoxImage.Exclamation); } } return(base.HwndSourceHook(hwnd, msg, wParam, lParam, ref handled)); }
protected override void OnStartup(StartupEventArgs e) { // Error handling: AppDomain.CurrentDomain.UnhandledException += (s, args) => { var em = new ErrorMessage((Exception)args.ExceptionObject); em.ShowDialog(); }; // Set Priority: // TODO: Setting for this Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.AboveNormal; // Get ClArgs: InstanceManager.Instance.CommmandLineArgs = Environment.GetCommandLineArgs().ToList(); bool mutexCreated; var windowsIdentity = WindowsIdentity.GetCurrent(); string mutexName = windowsIdentity != null ? ("SkyJukebox::{" + windowsIdentity.Name + "}").Replace('\\', '|') : "SkyJukebox::{NoUser}"; _mutex = new Mutex(true, mutexName, out mutexCreated); Message = (int)NativeMethods.RegisterWindowMessage(mutexName); if (!mutexCreated) { if (!InstanceManager.Instance.CommmandLineArgs.Contains("--wait")) { _mutex = null; if (InstanceManager.Instance.CommmandLineArgs.Count > 1) { ClArgs.WriteClArgsToFile(Environment.GetCommandLineArgs()); NativeMethods.SendMessage(NativeMethods.HWND_BROADCAST, Message, IntPtr.Zero, IntPtr.Zero); //MessageBox.Show("Posted HWND message", "Debug", MessageBoxButton.OK, MessageBoxImage.Exclamation); } Current.Shutdown(); return; } if (!_mutex.WaitOne(20000)) { MessageBox.Show("Operation has timed out.", "Waiting for previous thread to exit", MessageBoxButton.OK, MessageBoxImage.Error); Current.Shutdown(); return; } if (!Mutex.TryOpenExisting(mutexName, MutexRights.FullControl, out _mutex)) { _mutex = new Mutex(true, mutexName, out mutexCreated); if (!mutexCreated) { MessageBox.Show("Previous thread exited, but this thread failed to gain access to the mutex or create a new one!", "Waiting for previous thread to exit", MessageBoxButton.OK, MessageBoxImage.Error); Current.Shutdown(); return; } } } base.OnStartup(e); // Order is important! // Load MiniPlayer MainWindow = InstanceManager.Instance.MiniPlayerInstance = new MiniPlayer(); // Load PlaylistEditor InstanceManager.Instance.PlaylistEditorInstance = new PlaylistEditor(); // Load SettingsWindow InstanceManager.Instance.SettingsWindowInstance = new SettingsWindow(); // Continue MainWindow.Show(); }