Example #1
0
 internal PauseToken(PauseTokenSource source)
 {
     _source = source;
 }
Example #2
0
 internal PauseToken(PauseTokenSource source)
 {
     _source = source;
 }
        public async Task SendBulkMailAsync(IEnumerable<MailMessage> emails)
        {
            _pauseTokenSource = new PauseTokenSource();
            _cancellationTokenSource = new CancellationTokenSource();
            var ct = _cancellationTokenSource.Token;
            var mails = emails.ToArray();
            var sentMail = 0;
            var sendDelay = Task.Delay(1000, ct);
            var sendComplete = 0;

            foreach (var mail in mails)
            {
                await SendMailAsync(mail);
                // Report finish after 
                OnSendBulkMailProgress(++sendComplete, mails.Length);

                // Check send rate
                sentMail++;
                if (sentMail >= MaxSendRate)
                {
                    await sendDelay;
                    sentMail = 0;
                    sendDelay = Task.Delay(1000, ct);
                }

                // Cancel
                if (ct.IsCancellationRequested)
                {
                    break;
                }

                // Pause
                await _pauseTokenSource.Token.WaitWhilePausedAsync();
            }
        }