ShareX/ShareX.HelpersLib/SingleInstanceApplication/ApplicationInstanceManager.cs

175 lines
6.1 KiB
C#
Raw Normal View History

2014-12-31 23:07:19 +13:00
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
2016-01-04 04:16:01 +13:00
Copyright (c) 2007-2016 ShareX Team
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/
#endregion License Information (GPL v3)
using ShareX.HelpersLib;
using System;
using System.Diagnostics;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Ipc;
using System.Threading;
namespace SingleInstanceApplication
{
public class ApplicationInstanceManager : IDisposable
{
private static string AppName = "ShareX";
private static string EventName = string.Format("{0}-{1}", Environment.MachineName, AppName);
private static string SemaphoreName = string.Format("{0}{1}", EventName, "Semaphore");
2016-03-18 13:10:56 +13:00
public bool IsFirstInstance { get; private set; }
private Mutex mutex;
private Semaphore semaphore;
2016-03-18 13:10:56 +13:00
private IpcServerChannel serverChannel;
public ApplicationInstanceManager(bool isMultiInstance, string[] args, EventHandler<InstanceCallbackEventArgs> singleInstanceCallback)
{
mutex = new Mutex(false, "82E6AC09-0FEF-4390-AD9F-0DD3F5561EFC"); // Specific mutex required for installer
try
{
2016-03-18 13:10:56 +13:00
IsFirstInstance = mutex.WaitOne(100, false);
2016-03-18 13:10:56 +13:00
if (!IsFirstInstance && !isMultiInstance)
{
CreateMultipleInstance(singleInstanceCallback, args);
}
}
catch (AbandonedMutexException)
{
// Log the mutex was abandoned in another process, it will still get acquired
DebugHelper.WriteLine("Single instance mutex found abandoned from another process");
2016-03-18 13:10:56 +13:00
IsFirstInstance = true;
}
CreateFirstInstance(singleInstanceCallback);
}
public void Dispose()
{
2016-03-18 13:10:56 +13:00
if (IsFirstInstance)
{
2016-03-18 13:10:56 +13:00
if (mutex != null)
{
mutex.ReleaseMutex();
}
if (serverChannel != null)
{
ChannelServices.UnregisterChannel(serverChannel);
}
if (semaphore != null)
{
semaphore.Close();
}
}
}
private void CreateFirstInstance(EventHandler<InstanceCallbackEventArgs> callback)
{
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 EventWaitHandle was created
if (!createdNew)
{
return;
}
semaphore = new Semaphore(1, 1, SemaphoreName);
ThreadPool.RegisterWaitForSingleObject(eventWaitHandle, WaitOrTimerCallback, callback, Timeout.Infinite, false);
RegisterRemoteType(AppName);
}
}
private void CreateMultipleInstance(EventHandler<InstanceCallbackEventArgs> callback, string[] args)
{
InstanceProxy.CommandLineArgs = args;
using (EventWaitHandle eventWaitHandle = EventWaitHandle.OpenExisting(EventName))
{
semaphore = Semaphore.OpenExisting(SemaphoreName);
semaphore.WaitOne();
UpdateRemoteObject(AppName);
if (eventWaitHandle != null)
{
eventWaitHandle.Set();
}
}
Environment.Exit(0);
}
private void UpdateRemoteObject(string uri)
{
IpcClientChannel clientChannel = new IpcClientChannel();
ChannelServices.RegisterChannel(clientChannel, true);
InstanceProxy proxy = Activator.GetObject(typeof(InstanceProxy), string.Format("ipc://{0}{1}/{1}", Environment.MachineName, uri)) as InstanceProxy;
if (proxy != null)
{
proxy.SetCommandLineArgs(InstanceProxy.CommandLineArgs);
}
ChannelServices.UnregisterChannel(clientChannel);
}
private void RegisterRemoteType(string uri)
{
2016-03-18 13:10:56 +13:00
serverChannel = new IpcServerChannel(Environment.MachineName + uri);
ChannelServices.RegisterChannel(serverChannel, true);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(InstanceProxy), uri, WellKnownObjectMode.Singleton);
}
private void WaitOrTimerCallback(object state, bool timedOut)
{
EventHandler<InstanceCallbackEventArgs> callback = state as EventHandler<InstanceCallbackEventArgs>;
2016-03-18 13:10:56 +13:00
if (callback != null)
{
2016-03-18 13:10:56 +13:00
try
{
callback(state, new InstanceCallbackEventArgs(InstanceProxy.CommandLineArgs));
}
finally
{
if (semaphore != null)
{
semaphore.Release();
}
}
}
}
}
}