Beispiel #1
0
        public static void WaitForMutexAndRenameFile(string mutexName, TimeSpan timeToWaitBeforeThrowingException, string oldFilePath, string newFilePath, bool overwrite)
        {
            Mutex mutex = CBB_CommonMisc.GetExistingOrCreateNewMutex(mutexName);

            if (!mutex.WaitOne(timeToWaitBeforeThrowingException, true))           //new TimeSpan(0, 0, 60)))
            {
                throw new Exception(String.Format("WaitForMutexAndRenameFile: Mutex {0} was locked more than {1}. That should not happen.", mutexName, timeToWaitBeforeThrowingException));
            }
            else
            {
                try
                {
                    if (!File.Exists(oldFilePath))
                    {
                        throw new Exception("WaitForMutexAndRenameFile: Source file not found: " + oldFilePath);
                    }

                    if (!overwrite && File.Exists(newFilePath))
                    {
                        throw new Exception(String.Format("WaitForMutexAndRenameFile: File {0} already exists. Overwriting not allowed by design.", newFilePath));
                    }

                    if (overwrite && File.Exists(newFilePath))
                    {
                        File.Delete(newFilePath);
                    }

                    File.Move(oldFilePath, newFilePath);
                }
                finally
                {
                    mutex.ReleaseMutex();
                }
            }
        }
Beispiel #2
0
        public static void AppendFile(string filePath, string text, ConcurrencyProtectionMechanism concurrencyProtectionMechanism, string mutexName = null)
        {
            switch (concurrencyProtectionMechanism)
            {
            case ConcurrencyProtectionMechanism.None:
                AppendFile(filePath, text);
                break;

            case ConcurrencyProtectionMechanism.Lock:
                if (_appendFileLockObject == null)
                {
                    _appendFileLockObject = new Dictionary <string, object>();
                }
                if (!_appendFileLockObject.ContainsKey(filePath.ToUpper()))
                {
                    _appendFileLockObject.Add(filePath.ToUpper(), new object());
                }
                lock (_appendFileLockObject[filePath.ToUpper()])
                {
                    AppendFile(filePath, text);
                }
                break;

            case ConcurrencyProtectionMechanism.Mutex:
                if (String.IsNullOrEmpty(mutexName))
                {
                    mutexName = "CBB_AppendFileMutex-" + filePath.ToUpper().Replace('\\', '_');
                    //throw new ArgumentException("When ConcurencyProtectionMechanism is mutex mutexName must be specified.");
                }
                Mutex theMutex = CBB_CommonMisc.GetExistingOrCreateNewMutex(mutexName);
                theMutex.WaitOne();
                try
                {
                    AppendFile(filePath, text);
                }
                finally
                {
                    theMutex.ReleaseMutex();
                }
                break;

            default: throw new Exception(String.Format("ConcurrencyProtectionMechanism '{0}' not recognised.", concurrencyProtectionMechanism));
            }
        }