Ejemplo n.º 1
0
        /// <summary>
        /// Wraps sharing violations that could occur on a file IO operation.
        /// </summary>
        /// <param name="action">The action to execute. May not be null.</param>
        /// <param name="exceptionsCallback">The exceptions callback. May be null.</param>
        /// <param name="retryCount">The retry count.</param>
        /// <param name="waitTime">The wait time in milliseconds.</param>
        public static void WrapSharingViolations(WrapSharingViolationsCallback action, WrapSharingViolationsExceptionsCallback exceptionsCallback = null, int retryCount = 10, int waitTime = 100)
        {
            if (action == null)
                throw new ArgumentNullException("action");

            for (int i = 0; i < retryCount; i++)
            {
                try
                {
                    action();
                    return;
                }
                catch (IOException ioe)
                {
                    if ((IsSharingViolation(ioe) && (i < (retryCount - 1))) || (ioe is FileNotFoundException))
                    {
                        var wait = true;
                        if (exceptionsCallback != null)
                        {
                            wait = exceptionsCallback(ioe, i, retryCount, waitTime);
                        }
                        if (wait)
                        {
                            System.Threading.Thread.Sleep(waitTime);
                        }
                    }
                    else
                    {
                        throw;
                    }
                }
            }
        }
        /// <summary>
        /// Wraps sharing violations that could occur on a file IO operation.
        /// </summary>
        /// <param name="action">The action to execute. May not be null.</param>
        /// <param name="exceptionsCallback">The exceptions callback. May be null.</param>
        /// <param name="retryCount">The retry count.</param>
        /// <param name="waitTime">The wait time in milliseconds.</param>
        public static void Wrap(WrapSharingViolationsCallback action, WrapSharingViolationsExceptionsCallback exceptionsCallback, int retryCount, int waitTime)
        {
            if (action == null)
            {
                throw new ArgumentNullException(nameof(action));
            }

            for (int i = 0; i < retryCount; i++)
            {
                try
                {
                    action();
                    return;
                }
                catch (IOException ioe)
                {
                    if ((IsSharingViolation(ioe)) && (i < (retryCount - 1)))
                    {
                        bool wait = true;
                        if (exceptionsCallback != null)
                        {
                            wait = exceptionsCallback(ioe, i, retryCount, waitTime);
                        }
                        if (wait)
                        {
                            System.Threading.Thread.Sleep(waitTime);
                        }
                    }
                    else
                    {
                        throw;
                    }
                }
            }
        }
Ejemplo n.º 3
0
        public static T WrapSharingViolations <T>(Func <T> action, WrapSharingViolationsExceptionsCallback exceptionsCallback, int retryCount, int waitTime, bool throwOnError)
        {
            if (action == null)
            {
                throw new ArgumentNullException("action");
            }

            for (int i = 0; i < retryCount; i++)
            {
                try
                {
                    return(action());
                }
                catch (Exception e)
                {
                    if ((IsSharingViolation(e as IOException) || e is UnauthorizedAccessException) &&
                        i < (retryCount - 1))
                    {
                        bool wait = true;
                        if (exceptionsCallback != null)
                        {
                            wait = exceptionsCallback(e, i, retryCount, waitTime);
                        }

                        if (wait)
                        {
                            Thread.Sleep(waitTime);
                        }
                    }
                    else
                    {
                        if (throwOnError)
                        {
                            throw;
                        }

                        // else continue
                    }
                }
            }
            return(default(T));
        }