mirror of
https://github.com/ckaczor/Common.git
synced 2026-02-12 19:18:34 -05:00
Initial commit
This commit is contained in:
40
Helpers/ApplicationIsolation.cs
Normal file
40
Helpers/ApplicationIsolation.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
using System;
|
||||
using System.Reflection;
|
||||
using System.Security.AccessControl;
|
||||
using System.Security.Principal;
|
||||
using System.Threading;
|
||||
|
||||
namespace Common.Helpers
|
||||
{
|
||||
public static class ApplicationIsolation
|
||||
{
|
||||
public static IDisposable GetIsolationHandle()
|
||||
{
|
||||
string applicationName = Assembly.GetEntryAssembly().GetName().Name;
|
||||
|
||||
return GetIsolationHandle(applicationName);
|
||||
}
|
||||
|
||||
public static IDisposable GetIsolationHandle(string applicationName)
|
||||
{
|
||||
// Create mutex security settings
|
||||
MutexSecurity mutexSecurity = new MutexSecurity();
|
||||
|
||||
// Create a world security identifier
|
||||
SecurityIdentifier securityIdentifier = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
|
||||
|
||||
// Create an access rule for the mutex
|
||||
MutexAccessRule mutexAccessRule = new MutexAccessRule(securityIdentifier, MutexRights.FullControl, AccessControlType.Allow);
|
||||
|
||||
// Add the access rule to the mutex security settings
|
||||
mutexSecurity.AddAccessRule(mutexAccessRule);
|
||||
|
||||
// Create the mutex and see if it was newly created
|
||||
bool createdNew;
|
||||
Mutex mutex = new Mutex(false, applicationName, out createdNew, mutexSecurity);
|
||||
|
||||
// Return the mutex if it is new
|
||||
return createdNew ? mutex : null;
|
||||
}
|
||||
}
|
||||
}
|
||||
36
Helpers/DelayedMethod.cs
Normal file
36
Helpers/DelayedMethod.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using System.Timers;
|
||||
using System.Windows.Threading;
|
||||
|
||||
namespace Common.Helpers
|
||||
{
|
||||
public class DelayedMethod
|
||||
{
|
||||
public delegate void DelayedMethodDelegate();
|
||||
|
||||
private readonly Timer _delayTimer;
|
||||
private readonly DelayedMethodDelegate _methodDelegate;
|
||||
private readonly Dispatcher _dispatcher;
|
||||
|
||||
public DelayedMethod(int interval, DelayedMethodDelegate methodDelegate)
|
||||
{
|
||||
_dispatcher = Dispatcher.CurrentDispatcher;
|
||||
_methodDelegate = methodDelegate;
|
||||
|
||||
_delayTimer = new Timer(interval) { AutoReset = false };
|
||||
_delayTimer.Elapsed += handleDelayTimerElapsed;
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
_delayTimer.Stop();
|
||||
_delayTimer.Start();
|
||||
}
|
||||
|
||||
private void handleDelayTimerElapsed(object sender, ElapsedEventArgs e)
|
||||
{
|
||||
// Make sure we're on the right thread
|
||||
if (!_dispatcher.CheckAccess())
|
||||
_dispatcher.Invoke(_methodDelegate);
|
||||
}
|
||||
}
|
||||
}
|
||||
18
Helpers/Reflection.cs
Normal file
18
Helpers/Reflection.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
namespace Common.Helpers
|
||||
{
|
||||
public static class Reflection
|
||||
{
|
||||
public static string GetPropertyName<T>(Expression<Func<T>> propertyExpression)
|
||||
{
|
||||
MemberExpression memberExpression = (propertyExpression.Body as MemberExpression);
|
||||
|
||||
if (memberExpression == null)
|
||||
return null;
|
||||
|
||||
return memberExpression.Member.Name;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user