Skip to content

Commit e737596

Browse files
AppUI: Allow multiple instance running
This brings back the ability to run shell integrations while 7th is already opened.
1 parent 1be4824 commit e737596

File tree

3 files changed

+33
-238
lines changed

3 files changed

+33
-238
lines changed

AppUI/App.xaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
33
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
44
xmlns:local="clr-namespace:AppUI"
5-
StartupUri="Windows/MainWindow.xaml"
6-
Startup="Application_Startup">
5+
StartupUri="Windows/MainWindow.xaml">
76
<Application.Resources>
87
<ResourceDictionary>
98
<ResourceDictionary.MergedDictionaries>

AppUI/App.xaml.cs

Lines changed: 32 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -29,42 +29,49 @@ public partial class App : Application
2929

3030
public const string uniqueAppGuid = "F73958FA-160F-4185-AE8F-CF5B7EA89494";
3131

32-
public static Mutex uniqueMutex;
32+
private static Mutex _mutex;
3333

3434
private bool hasShownErrorWindow = false;
3535

3636
public App()
3737
{
3838
ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls13;
39+
}
40+
41+
protected override void OnStartup(StartupEventArgs e)
42+
{
43+
bool isNewInstance;
44+
45+
// Create a named mutex, which allows only one instance of the application.
46+
_mutex = new Mutex(true, uniqueAppGuid, out isNewInstance);
3947

40-
uniqueMutex = new Mutex(true, uniqueAppGuid, out bool gotMutex);
41-
GC.KeepAlive(App.uniqueMutex);
42-
43-
//TODO: Add support for .NET 8.0
44-
//if (SingleInstance.IsFirstInstance(uniqueAppGuid, true))
45-
//{
46-
// SingleInstance.OnSecondInstanceStarted += SingleInstance_OnSecondInstanceStarted;
47-
//}
48-
//else
49-
//{
50-
// // second instance so notify first instance
51-
// SingleInstance.NotifyFirstInstance(uniqueAppGuid);
52-
//}
53-
54-
if (!gotMutex)
48+
if (isNewInstance)
5549
{
56-
App.Current.Shutdown(); // only one instance of the app opened at a time so shutdown
57-
return;
50+
// This is the first instance - proceed normally.
51+
base.OnStartup(e);
52+
53+
// Enable Visual styles for Winform applications to support plugins that uses Winforms as a UI
54+
System.Windows.Forms.Application.EnableVisualStyles();
55+
System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(false);
56+
57+
// check if default language saved in app settings; otherwise detect language from thread
58+
string defaultLang = Sys.Settings.AppLanguage;
59+
60+
if (string.IsNullOrWhiteSpace(defaultLang))
61+
{
62+
defaultLang = GetCultureFromCurrentThread();
63+
}
64+
65+
SetLanguageDictionary(defaultLang);
66+
SetupExceptionHandling();
67+
}
68+
else
69+
{
70+
// A second instance is trying to start.
71+
ProcessCommandLineArgs(e.Args);
5872
}
5973
}
6074

61-
//TODO: Add support for .NET 8.0
62-
//private void SingleInstance_OnSecondInstanceStarted(object sender, SecondInstanceStartedEventArgs e)
63-
//{
64-
// // e.Args[0] = path to 7th Heaven .exe
65-
// ProcessCommandLineArgs(e.Args);
66-
//}
67-
6875
internal static void ProcessCommandLineArgs(string[] args, bool closeAfterProcessing = false)
6976
{
7077
bool hasLaunchCommand = args.Any(s => s.Equals("/LAUNCH", StringComparison.InvariantCultureIgnoreCase));
@@ -335,24 +342,6 @@ internal static string GetAppName()
335342
return "7th Heaven"; // default if can't find for some reason
336343
}
337344

338-
private void Application_Startup(object sender, StartupEventArgs e)
339-
{
340-
// Enable Visual styles for Winform applications to support plugins that uses Winforms as a UI
341-
System.Windows.Forms.Application.EnableVisualStyles();
342-
System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(false);
343-
344-
// check if default language saved in app settings; otherwise detect language from thread
345-
string defaultLang = Sys.Settings.AppLanguage;
346-
347-
if (string.IsNullOrWhiteSpace(defaultLang))
348-
{
349-
defaultLang = GetCultureFromCurrentThread();
350-
}
351-
352-
SetLanguageDictionary(defaultLang);
353-
SetupExceptionHandling();
354-
}
355-
356345
private void SetupExceptionHandling()
357346
{
358347
AppDomain.CurrentDomain.UnhandledException += (s, e) =>

AppUI/Classes/WCF/ServiceHost.cs

Lines changed: 0 additions & 193 deletions
This file was deleted.

0 commit comments

Comments
 (0)