locked
How to run code in a separate process RRS feed

  • Question

  • User-1317932780 posted

    I am not sure exactly which topic this post should go under...

    Here is what I am doing.

    I have a web form where a person will edit their blog article. At some point, once they are done editing, they can click a button "Publish Blog Now".

    Once the blog is published in the click event on the server side I am doing a query to get a list of subscriber email address.

    These are people who subscribed to this blogger and wish to receive an email notification whenever this person publishes a new blog.

    What I just realized today is that my hosting provider only allows me to send a maximum of 200 emails per hour. Which means in my loop I need to sleep for roughly 20 seconds between each email notification sent. But I dont want the user who clicked the publish button to have to sit there and wait while that process is going.

    How can I return to the user but yet continue to run some code on the server side to send out the emails in the background even if user closes web browser?

    This is an ASP.NET web application targeting .net 4.0 and I am using c# as my back-end language and VS2010 as my development tool.

     

    Thanks

    Wayne

    Wednesday, November 3, 2010 6:14 PM

Answers

  • User1546878023 posted

    You can do something like

     

     public void Sendmails()
                {
                    System.Threading.Thread t = new System.Threading.Thread(SendMailsAsync);
                    t.Start();
                }
                private void SendMailsAsync()
                {
                    //Send emails
                }

     

    But you need to be aware that the webserver process might be killed if you do not recieve any webrequests and therefore you might lose some mails. So I would recommend that you store your list of recipients to a file/database and then start a thread to send out the mails Then you can add a method in global.asax in application_start some logic to look at your file/database and see if there were some mails that were not sent correctly and then send them at that timeHope it gives you some hint as to what you could do

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 3, 2010 8:58 PM
  • User-952121411 posted

    Currently there are only about 10 subscribers but I am hoping the number will increase over time.
     

    Just keep this in mind because as your users scale, this primitive approach (stalling) may end up backfiring on you in the future.

    However I have never used the global.asax code before so I am not sure how to handle that.

    Global.asax is a file within your ASP.NET application (you might have to add it manually - new versions of VS.NET do not add it to projects automatically to some template types) that conatains methods for handing application level events. You can find a more detailed explination here:

    Global.asax File:

    http://msdn.microsoft.com/en-us/library/1xaas8a2(VS.71).aspx

    Global.asax Syntax:

    http://msdn.microsoft.com/en-us/library/2027ewzw.aspx

    For a life cycle of the Application events, scroll down to the section named 'Life Cycle Events and the Global.asax file' in the following link:

    http://msdn.microsoft.com/en-us/library/ms178473.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 4, 2010 11:56 AM

All replies

  • User1546878023 posted

    You can do something like

     

     public void Sendmails()
                {
                    System.Threading.Thread t = new System.Threading.Thread(SendMailsAsync);
                    t.Start();
                }
                private void SendMailsAsync()
                {
                    //Send emails
                }

     

    But you need to be aware that the webserver process might be killed if you do not recieve any webrequests and therefore you might lose some mails. So I would recommend that you store your list of recipients to a file/database and then start a thread to send out the mails Then you can add a method in global.asax in application_start some logic to look at your file/database and see if there were some mails that were not sent correctly and then send them at that timeHope it gives you some hint as to what you could do

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 3, 2010 8:58 PM
  • User-952121411 posted

    What I just realized today is that my hosting provider only allows me to send a maximum of 200 emails per hour. Which means in my loop I need to sleep for roughly 20 seconds between each email notification sent. But I dont want the user who clicked the publish button to have to sit there and wait while that process is going.

     

    Well the 1st question might be, how often in general do you need to initiate this process (someone updates their blog), and how many recipients in general need to have the email sent? If someone is updating their blog every 5 minutes and there are 1000 recipients, you are going to have a major backlog jam of sending emails with the 200 per hour imposed limit. If the scenario is anything like this, you will want to look into using a different SMTP server to send the emails.

    Thursday, November 4, 2010 8:50 AM
  • User-1317932780 posted

    I think the thread solution should work. However I have never used the global.asax code before so I am not sure how to handle that.

    Do I just create a new unit and give it that name? is there a special way to create that unit? I guess I can search for how to use global.aspx and probably find some answer. If you have any quick tips I would greatly appriciate it.

     

    For atconway, to answer your questions, this only occurs when the blogger "publishes a new blog" which in this case is about once every other month. Currently there are only about 10 subscribers but I am hoping the number will increase over time.

    Thursday, November 4, 2010 11:43 AM
  • User-952121411 posted

    Currently there are only about 10 subscribers but I am hoping the number will increase over time.
     

    Just keep this in mind because as your users scale, this primitive approach (stalling) may end up backfiring on you in the future.

    However I have never used the global.asax code before so I am not sure how to handle that.

    Global.asax is a file within your ASP.NET application (you might have to add it manually - new versions of VS.NET do not add it to projects automatically to some template types) that conatains methods for handing application level events. You can find a more detailed explination here:

    Global.asax File:

    http://msdn.microsoft.com/en-us/library/1xaas8a2(VS.71).aspx

    Global.asax Syntax:

    http://msdn.microsoft.com/en-us/library/2027ewzw.aspx

    For a life cycle of the Application events, scroll down to the section named 'Life Cycle Events and the Global.asax file' in the following link:

    http://msdn.microsoft.com/en-us/library/ms178473.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 4, 2010 11:56 AM
  • User-1573135047 posted

    I take it this is a blog your wrote in .net??

    Re: the 200 emails per hour... does 1 email going to 200 *recipients* count as 200, or 1 email?

    You could also do the emailing from SQL (assuming you have access to sql) and just add the e-mails to some queue table.


    Thursday, November 4, 2010 3:34 PM
  • User-1317932780 posted

    rareddy, You have brought up a very interesting point. I guess I could try to add multiple emails to the BCC of the email and send 1 email to like 10 or 20 people in 1 shot. I wonder what the limits are on number of BCC recipients at one time or if it is just how long that string can be.

     

    But even more important, your comment about having SQL send the emails? What are you saying? I am using a MS-SQL database. And I am planning on taking dahla's advice and writing the content of the email and the receipents to a SQL table and flag them once they are sent. But are you suggesting that some how SQL Server can send emails?

    Saturday, January 22, 2011 8:56 AM
  • User1905075617 posted

    The idea of adding multiple receipents in bcc section is good besides that you can use sql server too to send emails

    http://www.sqlteam.com/article/sending-smtp-mail-using-a-stored-procedure . This can be a stroed procedure which is part of your job that runs once in 30 mins and job will find all susbcribers to whome mail is yet to be send and then fire them emails . This is will be faster approach as al the processing is happening in DB only so no need to open and close db connections . If you go with threading model approach the problem i see is if after updating my blog i closed my browser immediately such that my async method never got exectued then it will be a problem . Using sql job is always better , safer and faster option

     

    Cheers

    Mayur 

    Thursday, January 27, 2011 12:57 AM