开发者

scheduled emails in asp.net with c# - which event or which place in code behind

开发者 https://www.devze.com 2023-04-12 17:51 出处:网络
i want to send scheduled emails to all of my users at specific time every day! which event or which place should i use for sending 开发者_高级运维them in code behind?

i want to send scheduled emails to all of my users at specific time every day!

which event or which place should i use for sending 开发者_高级运维them in code behind?

my pages are base on master and content pages!

should i use page_Load of master page or content page (Default.aspx) / or should i use global.asax for this purpose?

thanks in advance


You should use the global.asax, start a thread on startup and use a time to send emails. Stop the thread when the application closes.

You have to keep the application alive to keep the thread alive. If you do not have any request of a long time your application is closed by the iis server and you will not send emails.

something like:

    private static bool stopProcessing;
    private static object __batchProcessLock;
    private static const int sleeptime = 2500;//2.5 seconds

    protected void Application_Start(object sender, EventArgs e)
    {
        Thread BatchThread = new Thread(Start);
    }

    protected void Application_End(object sender, EventArgs e)
    {
        Stop();
    }


    private static void Start()
    {
        lock (__batchProcessLock) // Be sure to run this only once in the application
        { 
            stopProcessing = false;
            while (!stopProcessing)
            {
                try
                {
                    //Do you stuff (check if it is time to send emails)
                }
                catch (Exception ex)
                {
                    //handle exception
                }
                Sleep(sleeptime); 
            }
        }
    }


    private static void Stop()
    {
        stopProcessing = true;
    }


Depending on the volume of traffic to your site and how accurate your timing must be another option in addition to @Peer's suggestion is to have some code that runs on each request. That code will check when the timed activity last ran (by checking in database or something) and then if it is gone today's time to send and it hasn't sent yet then you run your send procedure. Otherwise you go on as normal.

Of course you'll want to spawn a new thread to do the work so you don't hold up the user request while it does the e-mail sending.

Cons of this method are that it relies on somebody hitting your site to trigger. Pros of this method is that it can run late without any special code. The method of using timers will require additional code on first check to basically do what is done here to check if today's run has occured when you start it.

I will say again that I don't think this is the best way but if it absolutely has to be via ASP.NET web pages then this might be your best bet.

Though if you are allowed to have other code not in ASP.NET then you could write an app that will go to as special page on your website at the right time, keeping the timer code on an app on your computer (or a server or whatever). the special page will use some kind of security to ensure only you can access it (eg password protection or similar) and when you hit the page it does the e-mail stuff. This way the emailing code is in your ASP.NET pages but the actually scheduling code is in a more sensible location.

This may of course not be an option either.

0

精彩评论

暂无评论...
验证码 换一张
取 消