Ejemplo n.º 1
0
        private async Task <MutexOperationResult> ForceClearIfTimeoutExpired()
        {
            var result = new MutexOperationResult();

            try
            {
                var file = await GetFile();

                if (file != null)
                {
                    var currentTime   = DateTime.Now;
                    var fileTimeStamp = file.DateCreated;

                    if ((currentTime - fileTimeStamp).TotalMilliseconds > TIMEOUT_MSEC * 2)
                    {
                        var cleared = await ClearAsync();

                        result.Combine(cleared);
                        Debug.WriteLine("-------Force clear: " + cleared.ToString());
                    }
                }
                else
                {
                    result.Result = MutexOperationResultEnum.Free;
                }
            }
            catch
            {
                result.Result = MutexOperationResultEnum.ExceptionFileSystem | MutexOperationResultEnum.FailToClear;
            }

            return(result);
        }
Ejemplo n.º 2
0
        public async Task <MutexOperationResult> AcquireAsync(int milliseconds = 0)
        {
            var         result = new MutexOperationResult();
            StorageFile file   = null;

            try
            {
                file = await GetFile();
            }
            catch
            {
                result.Result = MutexOperationResultEnum.ExceptionFileSystem;
                return(result);
            }


            if (file == null)
            {
                try
                {
                    result.Result = MutexOperationResultEnum.Free;
                    var key = Guid.NewGuid().ToString();
                    file = await Folder.CreateFileAsync(FILE);

                    await FileIO.WriteTextAsync(file, key);

                    result.AcquisitionKey = key;
                    result.Result         = MutexOperationResultEnum.Acquired;
                    SetDeferredMutexRelease(result); // Will launch task which release mutex when timeout
                }
                catch (Exception)
                {
                    result.Result = MutexOperationResultEnum.ExceptionFileSystem;
                }
            }
            else
            {
                // First attempt to intercept mutex: force clear if mutex Acquired longer then TIMEOUT
                var forcedCleared = await ForceClearIfTimeoutExpired();

                if (forcedCleared.ResultIs(MutexOperationResultEnum.Cleared))
                {
                    var forcedAcquired = await AcquireAsync();

                    result.Combine(forcedCleared);
                    result.Combine(forcedAcquired);
                    return(result);
                }

                // Second attempt to intercept mutex: await specifyed delay
                if (milliseconds >= 1000)
                {
                    for (var i = 0; i < Math.Min(milliseconds, TIMEOUT_MSEC) / 1000; i++) // There is no need to wait longer then TIMEOUT,
                                                                                          // if mutex not released it indicates greater failure
                    {
                        await Task.Delay(1000);

                        var delayedAcquired = await AcquireAsync();

                        result.Combine(delayedAcquired);
                        if (delayedAcquired.ResultIs(MutexOperationResultEnum.Acquired))
                        {
                            return(result);
                        }
                    }
                    result.Result = MutexOperationResultEnum.FailToAcquire;
                }
            }

            return(result);
        }