本文整理汇总了C#中System.Security.AccessControl.MutexAccessRule类的典型用法代码示例。如果您正苦于以下问题:C# MutexAccessRule类的具体用法?C# MutexAccessRule怎么用?C# MutexAccessRule使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
MutexAccessRule类属于System.Security.AccessControl命名空间,在下文中一共展示了MutexAccessRule类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: UIMutex
public UIMutex(string mutexName)
{
pGlobalMutexName = mutexName;
// Create a string representing the current user.
string userName = Environment.UserDomainName + "\\" + Environment.UserName;
// Create a security object that grants no access.
MutexSecurity mutexSecurity = new MutexSecurity();
// Add a rule that grants the current user the right
// to enter or release the mutex.
MutexAccessRule mutexAccessRule = new MutexAccessRule(userName, MutexRights.FullControl, AccessControlType.Allow);
mutexSecurity.AddAccessRule(mutexAccessRule);
bool createdNew = false;
pMutex = new Mutex(false, pGlobalMutexName, out createdNew, mutexSecurity);
if (createdNew)
{
// loggingSystem.LogVerbose("New Mutex created {0}", pGlobalMutexName);
}
else
{
//loggingSystem.LogVerbose("Existing Mutex opened {0}", globalMutextName);
}
}
示例2: GrabMutex
public static Mutex GrabMutex(string name)
{
var mutexName = "kalixLuceneSegmentMutex_" + name;
try
{
return Mutex.OpenExisting(mutexName);
}
catch (WaitHandleCannotBeOpenedException)
{
var worldSid = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
var security = new MutexSecurity();
var rule = new MutexAccessRule(worldSid, MutexRights.FullControl, AccessControlType.Allow);
security.AddAccessRule(rule);
var mutexIsNew = false;
return new Mutex(false, mutexName, out mutexIsNew, security);
}
catch (UnauthorizedAccessException)
{
var m = Mutex.OpenExisting(mutexName, MutexRights.ReadPermissions | MutexRights.ChangePermissions);
var security = m.GetAccessControl();
var user = Environment.UserDomainName + "\\" + Environment.UserName;
var rule = new MutexAccessRule(user, MutexRights.Synchronize | MutexRights.Modify, AccessControlType.Allow);
security.AddAccessRule(rule);
m.SetAccessControl(security);
return Mutex.OpenExisting(mutexName);
}
}
示例3: InterProcessMutexLock
public InterProcessMutexLock(String mutexName)
{
try
{
_mutexName = mutexName;
try
{
_currentMutex = Mutex.OpenExisting(_mutexName);
}
catch (WaitHandleCannotBeOpenedException)
{
// grant everyone access to the mutex
var security = new MutexSecurity();
var everyoneIdentity = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
var rule = new MutexAccessRule(everyoneIdentity, MutexRights.FullControl, AccessControlType.Allow);
security.AddAccessRule(rule);
// make sure to not initially own it, because if you do it also acquires the lock
// we want to explicitly attempt to acquire the lock ourselves so we know how many times
// this object acquired and released the lock
_currentMutex = new Mutex(false, mutexName, out _created, security);
}
AquireMutex();
}
catch(Exception ex)
{
var exceptionString = String.Format("Exception in InterProcessMutexLock, mutex name {0}", mutexName);
Log.Error(this, exceptionString, ex);
throw ExceptionUtil.Rethrow(ex, exceptionString);
}
}
示例4: BankAccountMutex
// Note: configuration based on stackoverflow answer: http://stackoverflow.com/questions/229565/what-is-a-good-pattern-for-using-a-global-mutex-in-c
public BankAccountMutex(double money)
{
// get application GUID as defined in AssemblyInfo.cs
string appGuid = ((GuidAttribute)Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(GuidAttribute), false).GetValue(0)).Value.ToString();
// unique id for global mutex - Global prefix means it is global to the machine
string mutexId = string.Format("Global\\{{{0}}}", appGuid);
// Need a place to store a return value in Mutex() constructor call
bool createdNew;
// set up security for multi-user usage
// work also on localized systems (don't use just "Everyone")
var allowEveryoneRule = new MutexAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), MutexRights.FullControl, AccessControlType.Allow);
var securitySettings = new MutexSecurity();
securitySettings.AddAccessRule(allowEveryoneRule);
mutex = new Mutex(false, mutexId, out createdNew, securitySettings);
LogConsole("Setting initial amount of money: " + money);
if (money < 0)
{
LogConsole("The entered money quantity cannot be negative. Money: " + money);
throw new ArgumentException(GetMessageWithTreadId("The entered money quantity cannot be negative. Money: " + money));
}
this.bankMoney = money;
}
示例5: OnStartup
protected override void OnStartup(StartupEventArgs e)
{
// store mutex result
bool createdNew;
// allow multiple users to run it, but only one per user
var allowEveryoneRule = new MutexAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), MutexRights.FullControl, AccessControlType.Allow);
var securitySettings = new MutexSecurity();
securitySettings.AddAccessRule(allowEveryoneRule);
// create mutex
_instanceMutex = new Mutex(true, @"Global\MercurialForge_Mastery", out createdNew, securitySettings);
// check if conflict
if (!createdNew)
{
MessageBox.Show("Instance of Mastery is already running");
_instanceMutex = null;
Application.Current.Shutdown();
return;
}
base.OnStartup(e);
MainWindow window = new MainWindow();
MainWindowViewModel viewModel = new MainWindowViewModel(window);
window.DataContext = viewModel;
window.Show();
}
示例6: Main
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(true);
string title = "KeyMagic";
bool beta = Properties.Settings.Default.BetaRelease;
if (beta) title += " (beta)";
string mutexName = "\u1000\u1001";
// http://stackoverflow.com/questions/229565/what-is-a-good-pattern-for-using-a-global-mutex-in-c/229567
using (var mutex = new Mutex(false, mutexName))
{
// edited by Jeremy Wiebe to add example of setting up security for multi-user usage
// edited by 'Marc' to work also on localized systems (don't use just "Everyone")
var allowEveryoneRule = new MutexAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), MutexRights.FullControl, AccessControlType.Allow);
var securitySettings = new MutexSecurity();
securitySettings.AddAccessRule(allowEveryoneRule);
mutex.SetAccessControl(securitySettings);
//edited by acidzombie24
var hasHandle = false;
try
{
try
{
// note, you may want to time out here instead of waiting forever
//edited by acidzombie24
//mutex.WaitOne(Timeout.Infinite, false);
hasHandle = mutex.WaitOne(100, false);
if (hasHandle == false) //another instance exist
{
IntPtr pWnd = NativeMethods.FindWindow(null, title);
if (pWnd != IntPtr.Zero)
{
NativeMethods.ShowWindow(pWnd, 0x05);
}
return;
}
}
catch (AbandonedMutexException)
{
// Log the fact the mutex was abandoned in another process, it will still get aquired
}
frmMain f = new frmMain();
Application.Run();
}
finally
{
//edit by acidzombie24, added if statemnet
if (hasHandle)
mutex.ReleaseMutex();
}
}
}
示例7: CreateMutexWithFullControlRights
public static Mutex CreateMutexWithFullControlRights(String name, out Boolean createdNew)
{
SecurityIdentifier securityIdentifier = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
MutexSecurity mutexSecurity = new MutexSecurity();
MutexAccessRule rule = new MutexAccessRule(securityIdentifier, MutexRights.FullControl, AccessControlType.Allow);
mutexSecurity.AddAccessRule(rule);
return new Mutex(false, name, out createdNew, mutexSecurity);
}
示例8: MutexSecurity
public static MutexSecurity MutexSecurity()
{
SecurityIdentifier user = GetEveryoneSID();
MutexSecurity result = new MutexSecurity();
MutexAccessRule rule = new MutexAccessRule(user, MutexRights.Synchronize | MutexRights.Modify | MutexRights.Delete, AccessControlType.Allow);
result.AddAccessRule(rule);
return result;
}
示例9: MutexHelper
// Methods
public MutexHelper(string mutexName)
{
bool flag;
this.pGlobalMutexName = mutexName;
string identity = Environment.UserDomainName + @"\" + Environment.UserName;
MutexSecurity mutexSecurity = new MutexSecurity();
MutexAccessRule rule = new MutexAccessRule(identity, MutexRights.FullControl, AccessControlType.Allow);
mutexSecurity.AddAccessRule(rule);
this.pMutex = new Mutex(false, this.pGlobalMutexName, out flag, mutexSecurity);
}
示例10: InitMutex
private void InitMutex(Guid appGuid)
{
var mutexId = string.Format("Global\\{{{0}}}", appGuid);
_mutex = new Mutex(false, mutexId);
var allowEveryoneRule = new MutexAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), MutexRights.FullControl, AccessControlType.Allow);
var securitySettings = new MutexSecurity();
securitySettings.AddAccessRule(allowEveryoneRule);
_mutex.SetAccessControl(securitySettings);
}
示例11: InitMutex
private void InitMutex()
{
var appGuid = ((GuidAttribute)Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(GuidAttribute), false).GetValue(0)).Value;
var mutexId = string.Format("Global\\{{{0}}}", appGuid);
_mutex = new Mutex(false, mutexId);
var allowEveryoneRule = new MutexAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), MutexRights.FullControl, AccessControlType.Allow);
var securitySettings = new MutexSecurity();
securitySettings.AddAccessRule(allowEveryoneRule);
_mutex.SetAccessControl(securitySettings);
}
示例12: BuildMutex
private static Mutex BuildMutex(string name)
{
name = name.Replace(":", "_");
name = name.Replace("/", "_");
name = name.Replace("\\", "_");
string mutexId = string.Format("Global\\{0}", name);
var allowEveryoneRule = new MutexAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), MutexRights.FullControl, AccessControlType.Allow);
var securitySettings = new MutexSecurity();
securitySettings.AddAccessRule(allowEveryoneRule);
bool createdNew;
var mutex = new Mutex(false, mutexId, out createdNew, securitySettings);
return mutex;
}
示例13: InitWaitHandle
private void InitWaitHandle() {
string mutexId = String.Format("Global\\{{{0}}}", _key);
try {
var allowEveryoneRule = new MutexAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), MutexRights.FullControl, AccessControlType.Allow);
var securitySettings = new MutexSecurity();
securitySettings.AddAccessRule(allowEveryoneRule);
bool wasCreated = false;
_waitHandle = new Mutex(false, mutexId, out wasCreated, securitySettings);
} catch (Exception) {
// We fallback to AutoResetEvent because Mutex isn't supported in medium trust.
_waitHandle = _namedLocks.GetOrAdd(_key, key => new AutoResetEvent(true));
}
}
示例14: GlobalMutex
/// <summary>
/// Creates a global mutex and allows everyone access to it.
/// </summary>
/// <param name="name">The name of the mutex to create in the Global namespace.</param>
public GlobalMutex(string name)
{
// Allow full control of the mutex for everyone so that other users will be able to
// create the same mutex and synchronise on it, if required.
var allowEveryoneRule = new MutexAccessRule(
new SecurityIdentifier(WellKnownSidType.WorldSid, null),
MutexRights.FullControl,
AccessControlType.Allow);
var securitySettings = new MutexSecurity();
securitySettings.AddAccessRule(allowEveryoneRule);
bool createdNew;
// Use the Global prefix to make it a system-wide object
mutex = new Mutex(false, @"Global\" + name, out createdNew, securitySettings);
}
示例15: TryGetMutex
public static bool TryGetMutex()
{
//source: http://stackoverflow.com/questions/229565/what-is-a-good-pattern-for-using-a-global-mutex-in-c
// get application GUID as defined in AssemblyInfo.cs
//string appGuid = "SjUpdater\\v" +Assembly.GetExecutingAssembly().GetName().Version.Major;
string appGuid = ((GuidAttribute)Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(GuidAttribute), false).GetValue(0)).Value.ToString();
// unique id for global mutex - Global prefix means it is global to the machine
string mutexId = string.Format("Global\\{{{0}}}", appGuid);
mutex = new Mutex(false, mutexId);
// edited by Jeremy Wiebe to add example of setting up security for multi-user usage
// edited by 'Marc' to work also on localized systems (don't use just "Everyone")
var allowEveryoneRule = new MutexAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null),
MutexRights.FullControl, AccessControlType.Allow);
var securitySettings = new MutexSecurity();
securitySettings.AddAccessRule(allowEveryoneRule);
mutex.SetAccessControl(securitySettings);
// edited by acidzombie24
try
{
try
{
// note, you may want to time out here instead of waiting forever
// edited by acidzombie24
// mutex.WaitOne(Timeout.Infinite, false);
hasHandle = mutex.WaitOne(5000, false);
if (hasHandle == false)
throw new TimeoutException("Timeout waiting for exclusive access");
}
catch (AbandonedMutexException)
{
// Log the fact the mutex was abandoned in another process, it will still get aquired
hasHandle = true;
}
return true;
}
catch
{
return false;
}
}