简体   繁体   English

在ASP.net网站C#中自动执行操作

[英]do a operation automatically in ASP.net website C#

I have a piece of code that I need to be run automatically in the background regardless of the page I am viewing. 我有一段代码,无论我查看的页面如何,都需要在后台自动运行它们。 For example if I am in the homepage or about page of a website I want a piece of code to still run automatically. 例如,如果我在主页有关网站的页面我想了一段代码仍自动运行。 To be precise I want to send a email notification every 30 minutes from the email class that I have made. 确切地说,我想每隔30分钟从我发送的电子邮件类别中发送一封电子邮件通知。 I am aware that similar thing can be done via windows service but I want the code to be in the website. 我知道可以通过Windows服务完成类似的操作,但我希望代码在网站中。

public class Email
{
    string emailFrom = "senderemail@gmail.com";
    string password = "yourpassword";        
    string smtpServer = "smtp.gmail.com";
    int port = 587;

    public void sendEmail(string emailTo, string subject, string body)
    {
        MailMessage msg = new MailMessage();
        msg.From = new MailAddress(emailFrom);
        msg.To.Add(emailTo);
        msg.Subject = subject;
        msg.Body = body;
        SmtpClient sc = new SmtpClient(smtpServer);
        sc.Port = port;
        sc.Credentials = new NetworkCredential(emailFrom, password);
        sc.EnableSsl = true;
        sc.Send(msg);
    }
}

In DOT.NET world Asynchronous invocations can be done in many ways like AJAX, AsyncHandlers etc. 在DOT.NET世界中,异步调用可以通过许多方式来完成,例如AJAX,AsyncHandlers等。

Here you can use "BackgroundWorker". 在这里您可以使用“ BackgroundWorker”。

void Application_Start(object sender, EventArgs e)
{
    // Code that runs on application startup
    BackgroundWorker worker = new BackgroundWorker();
    worker.DoWork += new DoWorkEventHandler(DoWork);
    worker.WorkerReportsProgress = false;
    worker.WorkerSupportsCancellation = true;
    worker.RunWorkerCompleted +=
           new RunWorkerCompletedEventHandler(WorkerCompleted);

    //Add this BackgroundWorker object instance to the cache (custom cache implementation)
    //so it can be cleared when the Application_End event fires.
    CacheManager.Add("BackgroundWorker", worker);

    // Calling the DoWork Method Asynchronously
    worker.RunWorkerAsync(); //we can also pass parameters to the async method....

}

private static void DoWork(object sender, DoWorkEventArgs e)
{

    // You code to send mail..
}

private static void WorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    BackgroundWorker worker = sender as BackgroundWorker;
    if (worker != null)
    {
        // sleep for 30 minutes and again call DoWork to send mail.
        System.Threading.Thread.Sleep(3600000);
        worker.RunWorkerAsync();
    }
}

void Application_End(object sender, EventArgs e)
{
    //  Code that runs on application shutdown
    //If background worker process is running then clean up that object.
    if (CacheManager.IsExists("BackgroundWorker"))
    {
        BackgroundWorker worker = (BackgroundWorker)CacheManager.Get("BackgroundWorker");
        if (worker != null)
            worker.CancelAsync();
    }
}

Hope this helps you... 希望这对您有帮助...

In your case you can try following code using threding 在您的情况下,您可以尝试使用threding遵循以下代码

var timer = new System.Threading.Timer((e) =>
{
    sendEmail(string emailTo, string subject, string body);   
}, null, 0, TimeSpan.FromMinutes(5).TotalMilliseconds);

Hope this will help you. 希望这会帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM