Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 159723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T10:54:33+00:00 2026-05-11T10:54:33+00:00

Sorry the title is a bit crappy, I couldn’t quite word it properly. Edit:

  • 0

Sorry the title is a bit crappy, I couldn’t quite word it properly.

Edit: I should note this is a console c# app

I’ve prototyped out a system that works like so (this is rough pseudo-codeish):

var collection = grabfromdb();  foreach (item in collection) {     SendAnEmail(); } 

SendAnEmail:

SmtpClient mailClient = new SmtpClient; mailClient.SendCompleted += new SendCompletedEventHandler(SendComplete); mailClient.SendAsync('the mail message'); 

SendComplete:

if (anyErrors) {     errorHandling() } else {     HitDBAndMarkAsSendOK();     } 

Obviously this setup is not ideal. If the initial collection has, say 10,000 records, then it’s going to new up 10,000 instances of smtpclient in fairly short order as quickly as it can step through the rows – and likely asplode in the process.

My ideal end game is to have something like 10 concurrent email going out at once.

A hacky solution comes to mind: Add a counter, that increments when SendAnEmail() is called, and decrements when SendComplete is sent. Before SendAnEmail() is called in the initial loop, check the counter, if it’s too high, then sleep for a small period of time and then check it again.

I’m not sure that’s such a great idea, and figure the SO hive mind would have a way to do this properly.

I have very little knowledge of threading and not sure if it would be an appropriate use here. Eg sending email in a background thread, first check the number of child threads to ensure there’s not too many being used. Or if there is some type of ‘thread throttling’ built in.


Update

Following in the advice of Steven A. Lowe, I now have:

  • A Dictionary holding my emails and a unique key (this is the email que
  • A FillQue Method, which populates the dictionary
  • A ProcessQue method, which is a background thread. It checks the que, and SendAsycs any email in the que.
  • A SendCompleted delegate which removes the email from the que. And calls FillQue again.

I’ve a few problems with this setup. I think I’ve missed the boat with the background thread, should I be spawning one of these for each item in the dictionary? How can I get the thread to ‘hang around’ for lack of a better word, if the email que empties the thread ends.


final update

I’ve put a ‘while(true) {}’ in the background thread. If the que is empty, it waits a few seconds and tries again. If the que is repeatedly empty, i ‘break’ the while, and the program ends… Works fine. I’m a bit worried about the ‘while(true)’ business though..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T10:54:34+00:00Added an answer on May 11, 2026 at 10:54 am

    Short Answer

    Use a queue as a finite buffer, processed by its own thread.

    Long Answer

    Call a fill-queue method to create a queue of emails, limited to (say) 10. Fill it with the first 10 unsent emails. Launch a thread to process the queue – for each email in the queue, send it asynch. When the queue is empty sleep for a while and check again. Have the completion delegate remove the sent or errored email from the queue and update the database, then call the fill-queue method to read more unsent emails into the queue (back up to the limit).

    You’ll only need locks around the queue operations, and will only have to manage (directly) the one thread to process the queue. You will never have more than N+1 threads active at once, where N is the queue limit.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry if the title is a bit nondescript, I couldn't really word it right.
Sorry for the title.....bit difficult to word what I really want to ask. Some
I'm sorry if the title is a bit confusing, but this is my problem:
Sorry if the title to this post is a bit confusing. My question is
Sorry the title is not very clear. This is a follow up to my
Sorry if the title isn't clear enough, but in Javascript you can do this
Sorry about the title, I couldn't think of a better way to describe the
Sorry for the title, I couldn't argue something better. Here is my question: Is
Sorry for dodgy title, couldn't think of anything better... Anyway, I have written a
Sorry for the bad title (couldn't think of a better way to describe it)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.