single-instance is supported natively on GTK, and the IPC helper has been moved to UWT WindowsForms engine
This commit is contained in:
parent
3be465d5bd
commit
693804dbb6
@ -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<bool>("SingleInstanceUniquePerDirectory", true))
|
||||
{
|
||||
// The single instance should be unique per directory
|
||||
INSTANCEID += System.Reflection.Assembly.GetEntryAssembly().Location;
|
||||
}
|
||||
if (!SingleInstanceManager.CreateSingleInstance(INSTANCEID, new EventHandler<SingleInstanceManager.InstanceCallbackEventArgs>(SingleInstanceManager_Callback))) return;
|
||||
|
||||
string[] args1 = Environment.GetCommandLineArgs();
|
||||
string[] args = new string[args1.Length - 1];
|
||||
Array.Copy(args1, 1, args, 0, args.Length);
|
||||
|
||||
@ -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<SingleInstanceManager.InstanceCallbackEventArgs> 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<SingleInstanceManager.InstanceCallbackEventArgs> eventHandler = state as EventHandler<SingleInstanceManager.InstanceCallbackEventArgs>;
|
||||
if (eventHandler != null)
|
||||
{
|
||||
eventHandler(state, new SingleInstanceManager.InstanceCallbackEventArgs(SingleInstanceManager.InstanceProxy.IsFirstInstance, SingleInstanceManager.InstanceProxy.CommandLineArgs));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -52,7 +52,6 @@
|
||||
<ItemGroup>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Runtime.Remoting" />
|
||||
<Reference Include="System.Configuration" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
@ -74,7 +73,6 @@
|
||||
<Compile Include="PropertyMapping.cs" />
|
||||
<Compile Include="RecentFileManager.cs" />
|
||||
<Compile Include="SessionManager.cs" />
|
||||
<Compile Include="SingleInstanceManager.cs" />
|
||||
<Compile Include="TemporaryFileManager.cs" />
|
||||
<Compile Include="Toolbox.cs" />
|
||||
<Compile Include="UnsavedDocumentOption.cs" />
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user