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 8349003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T07:51:37+00:00 2026-06-09T07:51:37+00:00

Certain actions that my web site’s users carry out result in the sending of

  • 0

Certain actions that my web site’s users carry out result in the sending of emails. The code that sends emails can block for a while, so I want to do this off their HTTP request handler’s thread.

Currently I’m using something like:

ThreadPool.QueueUserWorkItem(o => {
    try
    {
        email.Send();
    }
    catch (Exception ex)
    {
        _log.Error("Error sending email", ex);
    }
});

For the most part, this works. However the web site runs in a hosted environment where the app pool can be recycled.

Every once in a while I don’t receive an email that should have been sent, and I suspect that this work item on the threadpool’s queue is being dropped during application pool recycling.

How can I perform an ansync operation like this and guarantee that it will complete in such a case?

  • 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. Editorial Team
    Editorial Team
    2026-06-09T07:51:39+00:00Added an answer on June 9, 2026 at 7:51 am

    If your application runs in integrated mode, you can register your mail dispatcher service within the host environment. The host will notify your service before a recycling is done.
    The host will call your implementation of IRegisteredObject.Stop exactly 2 times. On the first call the host gives you the opportunity to finish the job. If the timeout is reached and your service has not removed itself from the host, then another call is made​, but this time only to notify that the recycling will be made with or without the consent of the service.

    This is an example (not tested) of how you can implement the Stop() method:

    public class MailDispatchService : IRegisteredObject
    {
        private AutoResetEvent _processQueueEvt = new AutoResetEvent();
        private ConcurrentQueue<MailMessage> _queue = new ConcurrentQueue<MailMessage>();
        private Thread _dispatcherThread;
        private volatile bool _enabled = true;
    
        #region Implementation of IRegisteredObject
    
        public void Stop(bool immediate)
        {
            if (_dispatcherThread != null && _dispatcherThread.IsAlive)
            {
                // it's not an immediate stop, we can wait for the queue to empty
                if (!immediate)
                {
                    // stop accepting new items in the send queue...
                    _enabled = false;
                    // awake dispatcher thread, so it can quit if the queue is empty
                    _processQueueEvt.Set();
                    // and wait for a while but not forever.
                    _dispatcherThread.Join(TimeSpan.FromSeconds(30));
                }
                else
                {
                    // host env will recycle now, nothing to do...
                    _dispatcherThread.Abort();
                }
            }
            // remove the service from host
            HostingEnvironment.UnregisterObject(this);
        }
    
        #endregion
    
        public void Start()
        {
            _dispatcherThread = new Thread(ProcessQueue);
            _dispatcherThread.Start();
        }
    
        private void ProcessQueue()
        {
            while (_enabled)
            {
                _processQueueEvt.WaitOne();
                MailMessage message;
                while (_queue.TryDequeue(out message)) { /* send mail ...*/}
            }
        }
    
        public void DispatchEmail(MailMessage message)
        {
            if (!_enabled) throw new Exception("....");
            _queue.Enqueue(message);
            _processQueueEvt.Set();
        }
    }
    

    Start the service and register it on the host.

    var mailService = new MailDispatchService();
    System.Web.Hosting.HostingEnvironment.RegisterObject(mailService);
    mailService.Start();
    
    var message = new MailMessage();
    mailService.DispatchEmail(message);   
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm developing a site that sends email notifications to users after certain events/user actions
We have a website that needs to perform certain actions when a pop-up is
I am creating a page that allows users access to a certain section of
I am creating a page that allows users access to a certain section of
When I press 'View source code' of a certain web page, it's kind of
I am creating a page that allows users access to a certain section of
I'm building a website that will send an email notification when certain actions are
I'm attempting to write an iPhone App that automates actions on a certain website.
We want to time how long certain actions run for in an ASP.NET MVC
I have a div with is replaced upon certain user actions. These actions are

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.