Пример #1
0
        /// <summary>
        /// Execute all actions list in parallel (threads)
        /// </summary>
        internal void Execute()
        {
            int remainingCounter = ActionsQueue.Count;

            if (remainingCounter > 0)
            {
                int maxParallelThreads = System.Environment.ProcessorCount * 2;
                int runningThreads     = 0;
                int threadIndex        = 1;

                while (remainingCounter > 0)
                {
                    // if actions queue is not empty and current running threads are less than the allowed max parallel threads count
                    if (ActionsQueue.Count > 0 && runningThreads < maxParallelThreads)
                    {
                        runningThreads++;
                        IAction downloadAction = ActionsQueue.Dequeue();

                        Task t = Task.Factory.StartNew(() =>
                        {
                            try
                            {
                                ProgressInfo fileProgressInfo        = new ProgressInfo();
                                fileProgressInfo.ProgressTransferred = this.ProgressDone;
                                fileProgressInfo.ProgressTotal       = this.ProgressTotal;
                                fileProgressInfo.FileIndex           = threadIndex;

                                ProgressInfoList.Add(threadIndex++, fileProgressInfo);

                                downloadAction.CopyFileItem(fileProgressInfo);
                            }
                            catch (Exception error)
                            {
                                Log.Logger.Instance.Error(error.Message);
                            }
                            remainingCounter--;
                            runningThreads--;
                        });
                    }

                    UpdateProgress();
                    Thread.Sleep(1000);
                }


                UpdateProgress();
                Thread.Sleep(1000);
            }
        }
        /// <summary>
        /// Execute all actions list in parallel (threads)
        /// </summary>
        internal void Execute()
        {
            int remainingCounter = ActionsQueue.Count;

            if (remainingCounter > 0)
            {
                int maxParallelThreads = System.Environment.ProcessorCount * 2;
                int runningThreads     = 0;
                int threadIndex        = 1;

                while (remainingCounter > 0)
                {
                    // if actions queue is not empty and current running threads are less than the allowed max parallel threads count
                    if (ActionsQueue.Count > 0 && runningThreads < maxParallelThreads)
                    {
                        Interlocked.Increment(ref runningThreads);
                        IAction downloadAction = ActionsQueue.Dequeue();
                        Task    t = Task.Factory.StartNew(async() =>
                        {
                            for (int i = 1; i <= 7; i++)
                            {
                                try
                                {
                                    ProgressInfo fileProgressInfo        = new ProgressInfo();
                                    fileProgressInfo.ProgressTransferred = this.ProgressDone;
                                    fileProgressInfo.ProgressTotal       = this.ProgressTotal;
                                    fileProgressInfo.FileIndex           = threadIndex;

                                    ProgressInfoList.Add(threadIndex++, fileProgressInfo);
                                    if (i > 1)
                                    {
                                        // This means that this is a retry. In that case force the operation
                                        // Otherwise file already exists error is thrown
                                        downloadAction.OpActionType = ActionType.Force;
                                        downloadAction.CopyFileItem(fileProgressInfo);
                                    }
                                    else
                                    {
                                        downloadAction.CopyFileItem(fileProgressInfo);
                                    }
                                    // Task completed, break out of the loop.
                                    Log.Logger.Instance.Debug("Action on Filename : " + downloadAction.FileName + " got completed in " + i + " try");
                                    break;
                                }
                                catch (AggregateException tce)
                                {
                                    // This means ShareFile Client Api has cancelled the task.
                                    // Retry the operation.
                                    Log.Logger.Instance.Error(tce.Message + " Upload/Download action for " + downloadAction.FileName + "\n" + tce.StackTrace);

                                    if (i == 7)
                                    {
                                        // No need to wait
                                        break;
                                    }
                                    // Wait for sometime before retrying the operation
                                    double timeToWait = Math.Pow(2, i);
                                    await Task.Delay(TimeSpan.FromSeconds(timeToWait));
                                }
                                catch (Exception e)
                                {
                                    // This means operation failed due to some other reasons.
                                    // No need to retry, break out of the loop.
                                    Log.Logger.Instance.Error(e.Message + "\n" + e.StackTrace);
                                    break;
                                }
                            }
                            Interlocked.Decrement(ref remainingCounter);
                            Interlocked.Decrement(ref runningThreads);
                        });
                    }

                    UpdateProgress();
                    Thread.Sleep(1000);
                }


                UpdateProgress();
                Thread.Sleep(1000);
            }
        }