mirror of
https://github.com/ShareX/ShareX.git
synced 2024-10-01 09:47:22 +13:00
Revert ApplicationInstanceManager for testing issue
This commit is contained in:
parent
7b302567f0
commit
3293de0703
1 changed files with 92 additions and 66 deletions
|
@ -23,11 +23,11 @@ You should have received a copy of the GNU General Public License
|
|||
|
||||
#endregion License Information (GPL v3)
|
||||
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.IO.Pipes;
|
||||
using System.Text;
|
||||
using System.Runtime.Remoting;
|
||||
using System.Runtime.Remoting.Channels;
|
||||
using System.Runtime.Remoting.Channels.Ipc;
|
||||
using System.Security.Permissions;
|
||||
using System.Threading;
|
||||
|
||||
namespace ShareX.HelpersLib
|
||||
|
@ -36,15 +36,15 @@ public class ApplicationInstanceManager : IDisposable
|
|||
{
|
||||
private static readonly string MutexName = "82E6AC09-0FEF-4390-AD9F-0DD3F5561EFC";
|
||||
private static readonly string AppName = "ShareX";
|
||||
private static readonly string PipeName = $"{Environment.MachineName}-{Environment.UserName}-{AppName}";
|
||||
private static readonly string SemaphoreName = PipeName + "Semaphore";
|
||||
private static readonly string EventName = string.Format("{0}-{1}-{2}", Environment.MachineName, Environment.UserName, AppName);
|
||||
private static readonly string SemaphoreName = string.Format("{0}{1}", EventName, "Semaphore");
|
||||
|
||||
public bool IsSingleInstance { get; private set; }
|
||||
public bool IsFirstInstance { get; private set; }
|
||||
|
||||
private Mutex mutex;
|
||||
private Semaphore semaphore;
|
||||
private NamedPipeServerStream pipeServer;
|
||||
private IpcServerChannel serverChannel;
|
||||
|
||||
public ApplicationInstanceManager(bool isSingleInstance, string[] args, EventHandler<InstanceCallbackEventArgs> callback)
|
||||
{
|
||||
|
@ -74,29 +74,45 @@ public ApplicationInstanceManager(bool isSingleInstance, string[] args, EventHan
|
|||
public void Dispose()
|
||||
{
|
||||
if (IsFirstInstance)
|
||||
{
|
||||
if (mutex != null)
|
||||
{
|
||||
mutex.ReleaseMutex();
|
||||
}
|
||||
|
||||
mutex.Dispose();
|
||||
semaphore?.Dispose();
|
||||
pipeServer?.Dispose();
|
||||
if (serverChannel != null)
|
||||
{
|
||||
ChannelServices.UnregisterChannel(serverChannel);
|
||||
}
|
||||
|
||||
if (semaphore != null)
|
||||
{
|
||||
semaphore.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void CreateFirstInstance(EventHandler<InstanceCallbackEventArgs> callback)
|
||||
{
|
||||
try
|
||||
{
|
||||
semaphore = new Semaphore(1, 1, SemaphoreName, out var createdNew);
|
||||
bool createdNew;
|
||||
|
||||
using (EventWaitHandle eventWaitHandle = new EventWaitHandle(false, EventResetMode.AutoReset, EventName, out createdNew))
|
||||
{
|
||||
// Mixing single instance and multi instance (via command line parameter) copies of the program can
|
||||
// result in CreateFirstInstance being called if it isn't really the first one. Make sure this is
|
||||
// really first instance by detecting if the semaphore was created
|
||||
// really first instance by detecting if EventWaitHandle was created
|
||||
if (!createdNew)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
CreateServer(callback);
|
||||
semaphore = new Semaphore(1, 1, SemaphoreName);
|
||||
ThreadPool.RegisterWaitForSingleObject(eventWaitHandle, WaitOrTimerCallback, callback, Timeout.Infinite, false);
|
||||
|
||||
RegisterRemoteType(AppName);
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
|
@ -108,11 +124,19 @@ private void CreateMultipleInstance(string[] args)
|
|||
{
|
||||
try
|
||||
{
|
||||
semaphore = Semaphore.OpenExisting(SemaphoreName);
|
||||
InstanceProxy.CommandLineArgs = args;
|
||||
|
||||
// Wait until the server is ready to accept data
|
||||
using (EventWaitHandle eventWaitHandle = EventWaitHandle.OpenExisting(EventName))
|
||||
{
|
||||
semaphore = Semaphore.OpenExisting(SemaphoreName);
|
||||
semaphore.WaitOne();
|
||||
SendDataToServer(args);
|
||||
UpdateRemoteObject(AppName);
|
||||
|
||||
if (eventWaitHandle != null)
|
||||
{
|
||||
eventWaitHandle.Set();
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
|
@ -122,67 +146,69 @@ private void CreateMultipleInstance(string[] args)
|
|||
Environment.Exit(0);
|
||||
}
|
||||
|
||||
private void SendDataToServer(string[] args)
|
||||
private void UpdateRemoteObject(string uri)
|
||||
{
|
||||
using (var pipeClient = new NamedPipeClientStream(".", PipeName, PipeDirection.Out))
|
||||
{
|
||||
pipeClient.Connect();
|
||||
IpcClientChannel clientChannel = new IpcClientChannel();
|
||||
ChannelServices.RegisterChannel(clientChannel, true);
|
||||
|
||||
var pipeData = new InstanceCallbackEventArgs
|
||||
{
|
||||
CommandLineArgs = args
|
||||
};
|
||||
InstanceProxy proxy = Activator.GetObject(typeof(InstanceProxy), string.Format("ipc://{0}{1}{2}/{2}", Environment.MachineName, Environment.UserName, uri)) as InstanceProxy;
|
||||
|
||||
var bytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(pipeData));
|
||||
pipeClient.Write(bytes, 0, bytes.Length);
|
||||
}
|
||||
if (proxy != null)
|
||||
{
|
||||
proxy.SetCommandLineArgs(InstanceProxy.CommandLineArgs);
|
||||
}
|
||||
|
||||
private void CreateServer(EventHandler<InstanceCallbackEventArgs> callback)
|
||||
{
|
||||
pipeServer = new NamedPipeServerStream(PipeName, PipeDirection.In, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous);
|
||||
pipeServer.BeginWaitForConnection(ConnectionCallback, callback);
|
||||
ChannelServices.UnregisterChannel(clientChannel);
|
||||
}
|
||||
|
||||
private void ConnectionCallback(IAsyncResult ar)
|
||||
private void RegisterRemoteType(string uri)
|
||||
{
|
||||
try
|
||||
{
|
||||
pipeServer.EndWaitForConnection(ar);
|
||||
}
|
||||
catch (ObjectDisposedException)
|
||||
{
|
||||
// Operation got aborted as part of program exit.
|
||||
return;
|
||||
serverChannel = new IpcServerChannel(Environment.MachineName + Environment.UserName + uri);
|
||||
ChannelServices.RegisterChannel(serverChannel, true);
|
||||
|
||||
RemotingConfiguration.RegisterWellKnownServiceType(typeof(InstanceProxy), uri, WellKnownObjectMode.Singleton);
|
||||
}
|
||||
|
||||
var callback = ar.AsyncState as EventHandler<InstanceCallbackEventArgs>;
|
||||
var sr = new StreamReader(pipeServer, Encoding.UTF8);
|
||||
try
|
||||
private void WaitOrTimerCallback(object state, bool timedOut)
|
||||
{
|
||||
EventHandler<InstanceCallbackEventArgs> callback = state as EventHandler<InstanceCallbackEventArgs>;
|
||||
|
||||
if (callback != null)
|
||||
{
|
||||
var data = sr.ReadToEnd();
|
||||
callback(this, JsonConvert.DeserializeObject<InstanceCallbackEventArgs>(data));
|
||||
}
|
||||
try
|
||||
{
|
||||
callback(state, new InstanceCallbackEventArgs(InstanceProxy.CommandLineArgs));
|
||||
}
|
||||
finally
|
||||
{
|
||||
// Close the existing server
|
||||
sr.Dispose();
|
||||
|
||||
// Create a new server
|
||||
CreateServer(callback);
|
||||
|
||||
// Signal that we are ready to accept a new connection
|
||||
if (semaphore != null)
|
||||
{
|
||||
semaphore.Release();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
|
||||
internal class InstanceProxy : MarshalByRefObject
|
||||
{
|
||||
public static string[] CommandLineArgs { get; internal set; }
|
||||
|
||||
public void SetCommandLineArgs(string[] commandLineArgs)
|
||||
{
|
||||
CommandLineArgs = commandLineArgs;
|
||||
}
|
||||
}
|
||||
|
||||
public class InstanceCallbackEventArgs : EventArgs
|
||||
{
|
||||
[JsonProperty]
|
||||
public string[] CommandLineArgs { get; internal set; }
|
||||
public string[] CommandLineArgs { get; private set; }
|
||||
|
||||
internal InstanceCallbackEventArgs(string[] commandLineArgs)
|
||||
{
|
||||
CommandLineArgs = commandLineArgs;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue