1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- using System;
- using System.Collections.Generic;
- using System.Configuration;
- using System.Data;
- using System.Linq;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Input;
- namespace PRSRecordingNotes
- {
- /// <summary>
- /// Interaction logic for App.xaml
- /// </summary>
- public partial class App : Application
- {
- #region Constants and Fields
- /// <summary>The event mutex name.</summary>
- private const string UniqueEventName = "27722504-3ECA-4493-A942-DF08FFD92500";
- /// <summary>The unique mutex name.</summary>
- private const string UniqueMutexName = "65B5069F-33AB-42A1-AB42-3E495097A859";
- /// <summary>The event wait handle.</summary>
- private EventWaitHandle eventWaitHandle;
- /// <summary>The mutex.</summary>
- private Mutex mutex;
- #endregion
- #region Methods
- /// <summary>The app on startup.</summary>
- /// <param name="sender">The sender.</param>
- /// <param name="e">The e.</param>
- private void AppOnStartup(object sender, StartupEventArgs e)
- {
- bool isOwned;
- this.mutex = new Mutex(true, UniqueMutexName, out isOwned);
- this.eventWaitHandle = new EventWaitHandle(false, EventResetMode.AutoReset, UniqueEventName);
- // So, R# would not give a warning that this variable is not used.
- GC.KeepAlive(this.mutex);
- if (isOwned)
- {
- // Spawn a thread which will be waiting for our event
- var thread = new Thread(
- () =>
- {
- while (this.eventWaitHandle.WaitOne())
- {
- Current.Dispatcher.BeginInvoke(
- (Action)(() => ((ScreenRecorderWindow)Current.MainWindow).BringToForeground()));
- }
- });
- // It is important mark it as background otherwise it will prevent app from exiting.
- thread.IsBackground = true;
- thread.Start();
- return;
- }
- // Notify other instance so it could bring itself to foreground.
- this.eventWaitHandle.Set();
- // Terminate this instance.
- this.Shutdown();
- }
- #endregion
- }
- }
|