Fixed License [skip ci]

This commit is contained in:
Robin 2016-03-25 12:11:05 +01:00
commit deb054b6fb

View file

@ -1,26 +1,25 @@
/* // Dapplo - building blocks for desktop applications
Dapplo - building blocks for desktop applications // Copyright (C) 2015-2016 Dapplo
Copyright (C) 2015-2016 Dapplo //
// For more information see: http://dapplo.net/
For more information see: http://dapplo.net/ // Dapplo repositories are hosted on GitHub: https://github.com/dapplo
Dapplo repositories are hosted on GitHub: https://github.com/dapplo //
// This file is part of Dapplo.Addons
This file is part of Dapplo.Addons //
// Dapplo.Addons is free software: you can redistribute it and/or modify
Dapplo.Addons is free software: you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as published by
it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or
the Free Software Foundation, either version 3 of the License, or // (at your option) any later version.
(at your option) any later version. //
// Dapplo.Addons is distributed in the hope that it will be useful,
Dapplo.Addons is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of
but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Lesser General Public License for more details.
GNU General Public License for more details. //
// You should have Config a copy of the GNU Lesser General Public License
You should have received a copy of the GNU General Public License // along with Dapplo.Addons. If not, see <http://www.gnu.org/licenses/lgpl.txt>.
along with Dapplo.Addons. If not, see <http://www.gnu.org/licenses/>.
*/
#region using
using log4net; using log4net;
using System; using System;
@ -28,29 +27,22 @@ using System.Security.AccessControl;
using System.Security.Principal; using System.Security.Principal;
using System.Threading; using System.Threading;
#endregion
namespace Greenshot.Helpers namespace Greenshot.Helpers
{ {
/// <summary> /// <summary>
/// This comes from Dapplo.Addons, which was created for Greenshot, this file is copied here as this is the only part we use in 1.2.8
/// This protects your resources or application from running more than once /// This protects your resources or application from running more than once
/// Simplifies the usage of the Mutex class, as described here: https://msdn.microsoft.com/en-us/library/System.Threading.Mutex.aspx /// Simplifies the usage of the Mutex class, as described here:
/// https://msdn.microsoft.com/en-us/library/System.Threading.Mutex.aspx
/// </summary> /// </summary>
public class ResourceMutex : IDisposable public class ResourceMutex : IDisposable
{ {
private static ILog LOG = LogManager.GetLogger(typeof(DestinationHelper)); private static readonly ILog Log = LogManager.GetLogger(typeof(DestinationHelper));
private readonly string _mutexId; private readonly string _mutexId;
private readonly string _resourceName; private readonly string _resourceName;
private Mutex _applicationMutex; private Mutex _applicationMutex;
/// <summary>
/// Test if the Mutex was created and locked.
/// </summary>
public bool IsLocked
{
get;
set;
}
/// <summary> /// <summary>
/// Private constructor /// Private constructor
/// </summary> /// </summary>
@ -62,6 +54,11 @@ namespace Greenshot.Helpers
_resourceName = resourceName ?? "some resource"; _resourceName = resourceName ?? "some resource";
} }
/// <summary>
/// Test if the Mutex was created and locked.
/// </summary>
public bool IsLocked { get; set; }
/// <summary> /// <summary>
/// Create a ResourceMutex for the specified mutex id and resource-name /// Create a ResourceMutex for the specified mutex id and resource-name
/// </summary> /// </summary>
@ -81,7 +78,7 @@ namespace Greenshot.Helpers
/// <returns>true if it worked, false if another instance is already running or something went wrong</returns> /// <returns>true if it worked, false if another instance is already running or something went wrong</returns>
public bool Lock() public bool Lock()
{ {
LOG.DebugFormat("{0} is trying to get Mutex {1}", _resourceName, _mutexId); Log.DebugFormat("{0} is trying to get Mutex {1}", _resourceName, _mutexId);
IsLocked = true; IsLocked = true;
// check whether there's an local instance running already, but use local so this works in a multi-user environment // check whether there's an local instance running already, but use local so this works in a multi-user environment
@ -100,7 +97,7 @@ namespace Greenshot.Helpers
// 2) if the mutex wasn't created new get the right to it, this returns false if it's already locked // 2) if the mutex wasn't created new get the right to it, this returns false if it's already locked
if (!createdNew && !_applicationMutex.WaitOne(100, false)) if (!createdNew && !_applicationMutex.WaitOne(100, false))
{ {
LOG.InfoFormat("{0} is already in use, mutex {1} is NOT locked for the caller", _resourceName, _mutexId); Log.InfoFormat("{0} is already in use, mutex {1} is NOT locked for the caller", _resourceName, _mutexId);
IsLocked = false; IsLocked = false;
// Clean up // Clean up
_applicationMutex.Close(); _applicationMutex.Close();
@ -108,42 +105,35 @@ namespace Greenshot.Helpers
} }
else else
{ {
if (createdNew) Log.InfoFormat(createdNew ? "{0} has created & claimed the mutex {1}" : "{0} has claimed the mutex {1}", _resourceName, _mutexId);
{
LOG.InfoFormat("{0} has created & claimed the mutex {1}", _resourceName, _mutexId);
}
else
{
LOG.InfoFormat("{0} has claimed the mutex {1}", _resourceName, _mutexId);
}
} }
} }
catch (AbandonedMutexException e) catch (AbandonedMutexException e)
{ {
// Another instance didn't cleanup correctly! // Another instance didn't cleanup correctly!
// we can ignore the exception, it happend on the "waitone" but still the mutex belongs to us // we can ignore the exception, it happend on the "waitone" but still the mutex belongs to us
LOG.WarnFormat("{0} didn't cleanup correctly, but we got the mutex {1}.", _resourceName, _mutexId); Log.WarnFormat("{0} didn't cleanup correctly, but we got the mutex {1}.", _resourceName, _mutexId);
LOG.Warn(e); Log.Warn(e);
} }
catch (UnauthorizedAccessException e) catch (UnauthorizedAccessException e)
{ {
LOG.ErrorFormat("{0} is most likely already running for a different user in the same session, can't create/get mutex {1} due to error.", _resourceName, _mutexId); Log.ErrorFormat("{0} is most likely already running for a different user in the same session, can't create/get mutex {1} due to error.", _resourceName, _mutexId);
LOG.Error(e); Log.Error(e);
IsLocked = false; IsLocked = false;
} }
catch (Exception ex) catch (Exception ex)
{ {
LOG.ErrorFormat("Problem obtaining the Mutex {1} for {0}, assuming it was already taken!", _resourceName, _mutexId); Log.ErrorFormat("Problem obtaining the Mutex {1} for {0}, assuming it was already taken!", _resourceName, _mutexId);
LOG.Error(ex); Log.Error(ex);
IsLocked = false; IsLocked = false;
} }
return IsLocked; return IsLocked;
} }
#region IDisposable Support #region IDisposable Support
// To detect redundant Dispose calls // To detect redundant Dispose calls
private bool _disposedValue = false; private bool _disposedValue;
/// <summary> /// <summary>
/// The real disposing code /// The real disposing code
@ -159,14 +149,13 @@ namespace Greenshot.Helpers
{ {
_applicationMutex.ReleaseMutex(); _applicationMutex.ReleaseMutex();
_applicationMutex = null; _applicationMutex = null;
LOG.InfoFormat("Released Mutex {0} for {1}", _mutexId, _resourceName); Log.InfoFormat("Released Mutex {0} for {1}", _mutexId, _resourceName);
} }
catch (Exception ex) catch (Exception ex)
{ {
LOG.ErrorFormat("Error releasing Mutex {0} for {1}", _mutexId, _resourceName); Log.ErrorFormat("Error releasing Mutex {0} for {1}", _mutexId, _resourceName);
LOG.Error(ex); Log.Error(ex);
} }
} }
_disposedValue = true; _disposedValue = true;
} }
@ -190,6 +179,7 @@ namespace Greenshot.Helpers
Dispose(true); Dispose(true);
GC.SuppressFinalize(this); GC.SuppressFinalize(this);
} }
#endregion #endregion
} }
} }