/// <summary> /// Отправляет данные /// </summary> /// <param name="worker">Отправитель</param> /// <param name="source"></param> /// <param name="settings"></param> private async void Process(IProcessPackageWorker worker, IProcessDataSource source, IExchangeSettings settings) { do { if (!settings.SilentCancel) { _cancellationToken.ThrowIfCancellationRequested(); } worker.GetProcessPackage(source, settings); var lastDateUpdated = DateTime.Now; if (!worker.IsBufferEmpty) { var tasks = new List <Task>(); for (var i = 0; i < settings.CountThreads; i++) { tasks.Add(ProcessAction(worker, source)); } while (tasks.Count > 0) { var finishedTask = await Task.WhenAny(tasks); tasks.Remove(finishedTask); } } else { var timeOut = (int)(DateTime.Now - lastDateUpdated).TotalMilliseconds; if (timeOut < settings.Timeout) { Thread.Sleep(settings.Timeout - timeOut); } } } while (!settings.StopServiceAfterFirstRun); }
/// <summary> /// Получает задачу отправки /// </summary> /// <param name="worker">Отправитель</param> /// <returns>Задача отправки</returns> private async Task ProcessAction(IProcessPackageWorker worker, IProcessDataSource source) { await Task.Run(() => worker.Process(source), _cancellationToken); }