Exemplo n.º 1
0
        private void UploadVisitsConcurrently(
            UploadContext uploadContext,
            List <FieldVisitInfo> visitsToAppend,
            List <FieldVisitInfo> duplicateVisits,
            ref bool isPartial,
            ref bool isFailure)
        {
            var semaphore = new SemaphoreSlim(Context.MaximumConcurrentRequests);

            var localIsPartial = false;
            var localIsFailure = false;

            Task.WhenAll(visitsToAppend.Select(async visit =>
            {
                using (await LimitedConcurrencyContext.EnterContextAsync(semaphore))
                {
                    await Task.Run(() =>
                                   UploadVisit(
                                       visit,
                                       uploadContext,
                                       () => localIsPartial = true,
                                       () => localIsFailure = true,
                                       () => duplicateVisits.Add(visit))
                                   , CancellationToken);
                }
            })).Wait(CancellationToken);

            isPartial |= localIsPartial;
            isFailure |= localIsFailure;
        }
Exemplo n.º 2
0
        private void UploadVisitsConcurrently(
            UploadContext uploadContext,
            List <FieldVisitInfo> visitsToAppend,
            List <FieldVisitInfo> duplicateVisits,
            ref bool isPartial,
            ref bool isFailure)
        {
            var semaphore = new SemaphoreSlim(Context.MaximumConcurrentRequests);

            var localIsPartial = false;
            var localIsFailure = false;

            Task.WhenAll(visitsToAppend.GroupBy(v => v.LocationInfo.LocationIdentifier).Select(async grouping =>
            {
                using (await LimitedConcurrencyContext.EnterContextAsync(semaphore))
                {
                    await Task.Run(() =>
                                   UploadLocationVisits(
                                       grouping
                                       .OrderBy(v => v.StartDate)
                                       .ThenBy(v => v.EndDate)
                                       .ToList(),
                                       grouping.Key,
                                       uploadContext,
                                       () => localIsPartial = true,
                                       () => localIsFailure = true,
                                       duplicateVisits.Add)
                                   , CancellationToken);
                }
            })).Wait(CancellationToken);

            isPartial |= localIsPartial;
            isFailure |= localIsFailure;
        }