From 693804dbb62202be9f4f0db4896bafb7fb0614bf Mon Sep 17 00:00:00 2001 From: Michael Becker Date: Mon, 8 Jun 2020 22:57:13 -0400 Subject: [PATCH] single-instance is supported natively on GTK, and the IPC helper has been moved to UWT WindowsForms engine --- .../UniversalEditor.UserInterface/Engine.cs | 25 ---- .../SingleInstanceManager.cs | 125 ------------------ .../UniversalEditor.UserInterface.csproj | 2 - 3 files changed, 152 deletions(-) delete mode 100644 Libraries/UniversalEditor.UserInterface/SingleInstanceManager.cs diff --git a/Libraries/UniversalEditor.UserInterface/Engine.cs b/Libraries/UniversalEditor.UserInterface/Engine.cs index 19161160..a7a59f91 100644 --- a/Libraries/UniversalEditor.UserInterface/Engine.cs +++ b/Libraries/UniversalEditor.UserInterface/Engine.cs @@ -825,23 +825,6 @@ namespace UniversalEditor.UserInterface } // UniversalDataStorage.Editor.WindowsForms.Program - private void SingleInstanceManager_Callback(object sender, SingleInstanceManager.InstanceCallbackEventArgs e) - { - if (!e.IsFirstInstance) - { - if (LastWindow != null) - { - Document[] documents = new Document[e.CommandLineArgs.Length - 1]; - for (int i = 1; i < e.CommandLineArgs.Length; i++) - { - documents[i - 1] = new Document(null, null, new FileAccessor(e.CommandLineArgs[i])); - } - - LastWindow.OpenFile(documents); - LastWindow.ActivateWindow(); - } - } - } public string ExpandRelativePath(string relativePath) { if (relativePath.StartsWith("~/")) @@ -1051,14 +1034,6 @@ namespace UniversalEditor.UserInterface Engine.mvarCurrentEngine = this; mvarRunning = true; - string INSTANCEID = GetType().FullName + "$2d429aa3371c421fb63b42525e51a50c$92751853175891031214292357218181357901238$"; - if (ConfigurationManager.GetValue("SingleInstanceUniquePerDirectory", true)) - { - // The single instance should be unique per directory - INSTANCEID += System.Reflection.Assembly.GetEntryAssembly().Location; - } - if (!SingleInstanceManager.CreateSingleInstance(INSTANCEID, new EventHandler(SingleInstanceManager_Callback))) return; - string[] args1 = Environment.GetCommandLineArgs(); string[] args = new string[args1.Length - 1]; Array.Copy(args1, 1, args, 0, args.Length); diff --git a/Libraries/UniversalEditor.UserInterface/SingleInstanceManager.cs b/Libraries/UniversalEditor.UserInterface/SingleInstanceManager.cs deleted file mode 100644 index eadd71fb..00000000 --- a/Libraries/UniversalEditor.UserInterface/SingleInstanceManager.cs +++ /dev/null @@ -1,125 +0,0 @@ -using System; -using System.Security.Permissions; -using System.Threading; -using System.Runtime.Remoting.Channels; -using System.Runtime.Remoting; -using System.Diagnostics; -using System.Runtime.Remoting.Channels.Ipc; - -internal static class SingleInstanceManager -{ - [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] - [Serializable] - private class InstanceProxy : MarshalByRefObject - { - public static bool IsFirstInstance - { - get; - internal set; - } - public static string[] CommandLineArgs - { - get; - internal set; - } - public void SetCommandLineArgs(bool isFirstInstance, string[] commandLineArgs) - { - SingleInstanceManager.InstanceProxy.IsFirstInstance = isFirstInstance; - SingleInstanceManager.InstanceProxy.CommandLineArgs = commandLineArgs; - } - } - public class InstanceCallbackEventArgs : EventArgs - { - public bool IsFirstInstance - { - get; - private set; - } - public string[] CommandLineArgs - { - get; - private set; - } - internal InstanceCallbackEventArgs(bool isFirstInstance, string[] commandLineArgs) - { - this.IsFirstInstance = isFirstInstance; - this.CommandLineArgs = commandLineArgs; - } - } - public static bool CreateSingleInstance(string name, EventHandler callback) - { - bool result; - switch (Environment.OSVersion.Platform) - { - case PlatformID.Unix: - case PlatformID.Xbox: - case PlatformID.MacOSX: - Console.WriteLine("IpcServerChannel constructor fails on linux and I don't know why"); - Console.WriteLine("Single Instance Mode Not Supported!!!"); - result = true; - return result; - } - name = name.Replace("\\", "_"); - name = name.Replace("/", "_"); - EventWaitHandle eventWaitHandle = null; - string name2 = string.Format("{0}-{1}", Environment.MachineName, name); - SingleInstanceManager.InstanceProxy.IsFirstInstance = false; - SingleInstanceManager.InstanceProxy.CommandLineArgs = Environment.GetCommandLineArgs(); - try - { - eventWaitHandle = EventWaitHandle.OpenExisting(name2); - } - catch - { - SingleInstanceManager.InstanceProxy.IsFirstInstance = true; - } - if (SingleInstanceManager.InstanceProxy.IsFirstInstance) - { - eventWaitHandle = new EventWaitHandle(false, EventResetMode.AutoReset, name2); - ThreadPool.RegisterWaitForSingleObject(eventWaitHandle, new WaitOrTimerCallback(SingleInstanceManager.WaitOrTimerCallback), callback, -1, false); - eventWaitHandle.Close(); - SingleInstanceManager.RegisterRemoteType(name); - } - else - { - SingleInstanceManager.UpdateRemoteObject(name); - if (eventWaitHandle != null) - { - eventWaitHandle.Set(); - } - Environment.Exit(0); - } - result = SingleInstanceManager.InstanceProxy.IsFirstInstance; - return result; - } - private static void UpdateRemoteObject(string uri) - { - IpcClientChannel chnl = new IpcClientChannel(); - ChannelServices.RegisterChannel(chnl, true); - SingleInstanceManager.InstanceProxy instanceProxy = Activator.GetObject(typeof(SingleInstanceManager.InstanceProxy), string.Format("ipc://{0}{1}/{1}", Environment.MachineName, uri)) as SingleInstanceManager.InstanceProxy; - if (instanceProxy != null) - { - instanceProxy.SetCommandLineArgs(SingleInstanceManager.InstanceProxy.IsFirstInstance, SingleInstanceManager.InstanceProxy.CommandLineArgs); - } - ChannelServices.UnregisterChannel(chnl); - } - private static void RegisterRemoteType(string uri) - { - IpcServerChannel serverChannel = new IpcServerChannel(Environment.MachineName + uri); - ChannelServices.RegisterChannel(serverChannel, true); - RemotingConfiguration.RegisterWellKnownServiceType(typeof(SingleInstanceManager.InstanceProxy), uri, WellKnownObjectMode.Singleton); - Process currentProcess = Process.GetCurrentProcess(); - currentProcess.Exited += delegate - { - ChannelServices.UnregisterChannel(serverChannel); - }; - } - private static void WaitOrTimerCallback(object state, bool timedOut) - { - EventHandler eventHandler = state as EventHandler; - if (eventHandler != null) - { - eventHandler(state, new SingleInstanceManager.InstanceCallbackEventArgs(SingleInstanceManager.InstanceProxy.IsFirstInstance, SingleInstanceManager.InstanceProxy.CommandLineArgs)); - } - } -} \ No newline at end of file diff --git a/Libraries/UniversalEditor.UserInterface/UniversalEditor.UserInterface.csproj b/Libraries/UniversalEditor.UserInterface/UniversalEditor.UserInterface.csproj index 72dde0dc..b45553ae 100644 --- a/Libraries/UniversalEditor.UserInterface/UniversalEditor.UserInterface.csproj +++ b/Libraries/UniversalEditor.UserInterface/UniversalEditor.UserInterface.csproj @@ -52,7 +52,6 @@ - @@ -74,7 +73,6 @@ -