Merge branch 'develop' into Better_Rounds_Handling

This commit is contained in:
Flamefire committed 2013-10-24 19:44:57 +02:00
commit fed140bc15
1 file changed
+31 -11
+31 -11
View File
@@ -20,6 +20,8 @@ using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
using Vocaluxe.Base;
using System.Runtime.ExceptionServices;
@@ -30,6 +32,12 @@ namespace Vocaluxe
{
static class CMainProgram
{
#if WIN
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool SetForegroundWindow(IntPtr hWnd);
#endif
private static CSplashScreen _SplashScreen;
[STAThread, HandleProcessCorruptedStateExceptions]
@@ -39,6 +47,9 @@ namespace Vocaluxe
{
AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionHandler;
AppDomain.CurrentDomain.AssemblyResolve += _AssemblyResolver;
// Close program if there is another instance running
if (!_EnsureSingleInstance())
return;
try
{
_Run(args);
@@ -59,14 +70,6 @@ namespace Vocaluxe
private static void _Run(string[] args)
{
// Close program if there is another instance running
if (!_EnsureSingleInstance())
{
//TODO: put it into language file
MessageBox.Show("Another Instance of Vocaluxe is already runnning!");
return;
}
Application.DoEvents();
try
@@ -304,12 +307,29 @@ namespace Vocaluxe
return null;
}
private static readonly Mutex _Mutex = new Mutex(false, Application.ProductName + "-SingleInstanceMutex");
private static bool _EnsureSingleInstance()
{
Process currentProcess = Process.GetCurrentProcess();
Process[] processes = Process.GetProcesses();
// wait a few seconds in case that the instance is just shutting down
if (!_Mutex.WaitOne(TimeSpan.FromSeconds(2), false))
{
//TODO: put it into language file
MessageBox.Show("Another Instance of Vocaluxe is already runnning!");
#if WIN
Process currentProcess = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName(currentProcess.ProcessName);
return processes.All(t => t.Id == currentProcess.Id || currentProcess.ProcessName != t.ProcessName);
foreach (var process in processes.Where(t => t.Id != currentProcess.Id))
{
var wnd = process.MainWindowHandle;
if (wnd != IntPtr.Zero)
SetForegroundWindow(wnd);
}
#endif
return false;
}
return true;
}
}
}