Browse Source

Fix #2597, fix #2579: Use "ILSpyInstance" mutex to wait until the first ILSpy instance is ready to receive WM_COPYDATA.

pull/2606/head
Siegfried Pammer 3 years ago
parent
commit
fe3fcc8e36
  1. 42
      ILSpy/App.xaml.cs
  2. 1
      ILSpy/MainWindow.xaml.cs

42
ILSpy/App.xaml.cs

@ -24,6 +24,7 @@ using System.Linq; @@ -24,6 +24,7 @@ using System.Linq;
using System.Reflection;
using System.Runtime.Loader;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Documents;
@ -34,7 +35,6 @@ using ICSharpCode.ILSpy.Options; @@ -34,7 +35,6 @@ using ICSharpCode.ILSpy.Options;
using Microsoft.VisualStudio.Composition;
using TomsToolbox.Wpf.Interactivity;
using TomsToolbox.Wpf.Styles;
namespace ICSharpCode.ILSpy
@ -46,6 +46,7 @@ namespace ICSharpCode.ILSpy @@ -46,6 +46,7 @@ namespace ICSharpCode.ILSpy
{
internal static CommandLineArguments CommandLineArguments;
internal static readonly IList<ExceptionData> StartupExceptions = new List<ExceptionData>();
internal static Mutex SingleInstanceMutex;
public static ExportProvider ExportProvider { get; private set; }
public static IExportProviderFactory ExportProviderFactory { get; private set; }
@ -60,12 +61,35 @@ namespace ICSharpCode.ILSpy @@ -60,12 +61,35 @@ namespace ICSharpCode.ILSpy
{
var cmdArgs = Environment.GetCommandLineArgs().Skip(1);
App.CommandLineArguments = new CommandLineArguments(cmdArgs);
if ((App.CommandLineArguments.SingleInstance ?? true) && !MiscSettingsPanel.CurrentMiscSettings.AllowMultipleInstances)
bool forceSingleInstance = (App.CommandLineArguments.SingleInstance ?? true)
&& !MiscSettingsPanel.CurrentMiscSettings.AllowMultipleInstances;
if (forceSingleInstance)
{
bool isFirst;
try
{
SingleInstanceMutex = new Mutex(initiallyOwned: true, @"Local\ILSpyInstance", out isFirst);
}
catch (WaitHandleCannotBeOpenedException)
{
isFirst = true;
}
if (!isFirst)
{
try
{
SingleInstanceMutex.WaitOne(10000);
}
catch (AbandonedMutexException)
{
// continue, there is no concurrent start happening.
}
}
cmdArgs = cmdArgs.Select(FullyQualifyPath);
string message = string.Join(Environment.NewLine, cmdArgs);
if (SendToPreviousInstance("ILSpy:\r\n" + message, !App.CommandLineArguments.NoActivate))
{
ReleaseSingleInstanceMutex();
Environment.Exit(0);
}
}
@ -87,6 +111,20 @@ namespace ICSharpCode.ILSpy @@ -87,6 +111,20 @@ namespace ICSharpCode.ILSpy
ILSpyTraceListener.Install();
}
internal static void ReleaseSingleInstanceMutex()
{
var mutex = SingleInstanceMutex;
SingleInstanceMutex = null;
if (mutex == null)
{
return;
}
using (mutex)
{
mutex.ReleaseMutex();
}
}
static Assembly ResolvePluginDependencies(AssemblyLoadContext context, AssemblyName assemblyName)
{
#if !NET472

1
ILSpy/MainWindow.xaml.cs

@ -329,6 +329,7 @@ namespace ICSharpCode.ILSpy @@ -329,6 +329,7 @@ namespace ICSharpCode.ILSpy
{
hwndSource.AddHook(WndProc);
}
App.ReleaseSingleInstanceMutex();
// Validate and Set Window Bounds
Rect bounds = Rect.Transform(sessionSettings.WindowBounds, source.CompositionTarget.TransformToDevice);
var boundsRect = new System.Drawing.Rectangle((int)bounds.Left, (int)bounds.Top, (int)bounds.Width, (int)bounds.Height);

Loading…
Cancel
Save