private void CleanUpMessages(object state) { var directory = (DirectoryInfo)state; bool createdNew; var mutexName = string.Concat(MutexCleanUpKey, ".", directory.Name); var accessControl = new MutexSecurity(); var sid = new SecurityIdentifier(WellKnownSidType.AuthenticatedUserSid, null); accessControl.SetAccessRule(new MutexAccessRule(sid, MutexRights.FullControl, AccessControlType.Allow)); using (var mutex = new Mutex(true, mutexName, out createdNew, accessControl)) { if (createdNew) { try { Thread.Sleep(fileTimeoutMilliseconds); } catch (ThreadInterruptedException) { } CleanUpMessages(directory); mutex.ReleaseMutex(); } } if (createdNew) { ThreadPool.QueueUserWorkItem(CleanUpMessages, directory); } }
public static void Main() { // Create a string representing the current user. string user = Environment.UserDomainName + "\\" + Environment.UserName; // Create a security object that grants no access. MutexSecurity mSec = new MutexSecurity(); // Add a rule that grants the current user the // right to enter or release the mutex and read the // permissions on the mutex. MutexAccessRule rule = new MutexAccessRule(user, MutexRights.Synchronize | MutexRights.Modify | MutexRights.ReadPermissions, AccessControlType.Allow); mSec.AddAccessRule(rule); // Add a rule that denies the current user the // right to change permissions on the mutex. rule = new MutexAccessRule(user, MutexRights.ChangePermissions, AccessControlType.Deny); mSec.AddAccessRule(rule); // Display the rules in the security object. ShowSecurity(mSec); // Create a rule that grants the current user // the full control over the mutex. Use the // SetAccessRule method to replace the // existing Allow rule with the new rule. rule = new MutexAccessRule(user, MutexRights.FullControl, AccessControlType.Allow); mSec.SetAccessRule(rule); ShowSecurity(mSec); }
/// <summary> /// This method is called within a seperate thread and deletes messages that are older than /// the pre-defined expiry time. /// </summary> /// <param name = "state"></param> private static void CleanUpMessages(object state) { var directory = (DirectoryInfo)state; // use a mutex to ensure only one listener system wide is running bool createdNew; string mutexName = string.Concat(mutexCleanUpKey, ".", directory.Name); var accessControl = new MutexSecurity(); var sid = new SecurityIdentifier(WellKnownSidType.AuthenticatedUserSid, null); accessControl.SetAccessRule(new MutexAccessRule(sid, MutexRights.FullControl, AccessControlType.Allow)); using (var mutex = new Mutex(true, mutexName, out createdNew, accessControl)) { // we this thread owns the Mutex then clean up otherwise exit. if (createdNew) { // wait for the specified timeout before attempting to clean directory try { Thread.Sleep(fileTimeoutMilliseconds); } catch (ThreadInterruptedException) { } CleanUpMessages(directory); // release the mutex mutex.ReleaseMutex(); } } if (createdNew) { // after mutex release add an additional thread for cleanup in case we're the last out // and there are now additional files to clean ThreadPool.QueueUserWorkItem(CleanUpMessages, directory); } }