Skip to content

Commit

Permalink
Prevent multiple instances from being run; make alt-tab handler async…
Browse files Browse the repository at this point in the history
…, preventing windows from taking over
  • Loading branch information
christianrondeau committed Jul 27, 2014
1 parent c7c7b2e commit 1817c3b
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion GoToWindow/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;

namespace GoToWindow
{
Expand All @@ -17,9 +19,18 @@ public partial class App : Application
{
private readonly IGoToWindowContext _context = new GoToWindowContext();
private InterceptAltTab _keyHandler;
private Mutex _mutex;

private void Application_Startup(object sender, StartupEventArgs e)
{
bool mutexCreated;
_mutex = new Mutex(true, "GoToWindow", out mutexCreated);
if (!mutexCreated)
{
Application.Current.Shutdown(1);
return;
}

TaskbarIcon trayIcon = new TaskbarIcon();
trayIcon.Icon = GoToWindow.Properties.Resources.AppIcon;
trayIcon.ToolTipText = "Go To Window";
Expand All @@ -36,13 +47,25 @@ private void Application_Startup(object sender, StartupEventArgs e)
_keyHandler = new InterceptAltTab(HandleAltTab);
}

delegate void CommandDelegate(object parameters);

private void HandleAltTab()
{
new OpenMainWindowCommand(_context).Execute(null);
var cmd = new OpenMainWindowCommand(_context);
Application.Current.Dispatcher.BeginInvoke(
new CommandDelegate(cmd.Execute),
DispatcherPriority.Normal,
new Object[] { null });
}

private void Application_Exit(object sender, ExitEventArgs e)
{
if (_mutex != null)
{
_mutex.ReleaseMutex();
_mutex.Dispose();
}

if (_keyHandler != null)
{
_keyHandler.Dispose();
Expand Down

0 comments on commit 1817c3b

Please sign in to comment.