How to create a job scheduler in asp.net application?



Hi
Before 6 months I was working on one web portal, there was requirement to auto send mail to trial register users and that site was hosted on shared server and we are using sql server 2005 express edition. So there was only one option to create a scheduler in asp.net application.

At first time I faced so much problems, at finally I did like this

Note: Here I have tested the concept with database.

Srep1: Create a “Global.asax”.

Step2: Create one Class i.e DoSomeWork and write the code for insert in database like this and create a table in database with field Id,msg, and sentDate to check this concept

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

/// <summary>
/// Summary description for DoSomeWork
/// </summary>
public class DoSomeWork
{

public void DoSomeWorkWithDB()
{

//code for storing in database
// You can also write code for sending mail to some one on some condition

using (SqlConnection con = new SqlConnection("Data Source=.\\sqlexpress;Initial Catalog=Test;Integrated Security=True"))
{
using (SqlCommand cmd = new SqlCommand("Insert into tblMsg(msg) values(@msg)", con))
{
cmd.Parameters.AddWithValue("@msg", "This is the test message");
con.Open();
cmd.ExecuteNonQuery();
con.Close();

}
}

}
}

Step3: In “Application_Start “ method of Global.asax file, use the timer concept and assign the time interval to run that block of code like this.

<%@ Application Language="C#" %>
<%@ Import Namespace="System.Timers" %>

<script runat="server">

void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
System.Timers.Timer myTimer = new System.Timers.Timer();
// Set the Interval to 10 seconds (10000 milliseconds).
myTimer.Interval = 10000;

myTimer.AutoReset = true;
myTimer.Elapsed += new ElapsedEventHandler(myTimer_Elapsed);
myTimer.Enabled = true;

}

public void myTimer_Elapsed(object source, System.Timers.ElapsedEventArgs e)
{

DoSomeWork obj1 = new DoSomeWork();
obj1.DoSomeWorkWithDB();
}

void Application_End(object sender, EventArgs e)
{
// Code that runs on application shutdown

}

void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs

}

void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new session is started

}

void Session_End(object sender, EventArgs e)
{

}

</script>

Step3: Now run the asp.net application. It will insert data in database in every 10 second.

Note: You have some better idea to do this task then feel free to share with me. You are always welcome.

Advertisements

10 Responses to “How to create a job scheduler in asp.net application?”

  1. Jai Says:

    Great work man!

    • Chandra Dev Says:

      Thank you jai. I m glad to know that you liked it.

      Regards
      Chandradev

  2. asdasdffasda Says:

    Wouldn’t this be quite waist-full running a timer all the time?

    • Chandra Dev Says:

      Yes. This one is not acceptable in big project.There generally we used to create window job scheduler.
      I have written one approach for doing that task.

  3. vijay Says:

    hi..
    If you use this type of tech..it will not affect that server response..bcz its keep on running on every 10 seconds…..For better choice go for window scheduler..tech

    • Chandra Dev Says:

      Thank you for sending your suggestion. I agree with you.But if somebody is working for small client and client has not their own server then we cannot use window job scheduler.

      In my sample, I m running the timer every 10 sec.we can also set the timer to run only one time in whole day by increasing time interval.

      • RohitRanjan Says:

        hi chandra i want one help from you currently i am working in one food corporation.see.i want to take automatic backup from all my shop i:e my company counter will you let m e know how to take automatic back up. my project based on asp.net2.0 version for database we are using sqlserver2005. if you solve this issue its a great pleasure for me.
        i try but not successful. kindly reply me aur mail me

      • Chandra Dev Says:

        Hi
        This is eassy task only. Please refer this URL. You get idea to solve this problem using different approach
        http://forums.asp.net/t/1864826.aspx/1?Timer+in+WCF
        Could you share some more information regarding your requirement, so that i can give you sample code in my free time.

  4. gra strategiczna online Says:

    Thanks for the auspicious writeup. It if truth be told used to be a enjoyment
    account it. Look advanced to more delivered agreeable from you!
    By the way, how could we keep up a correspondence?

  5. bouncy castle rental Says:

    whoah this blog is excellent i love reading your posts.
    Keep up the good work! You already know, a lot of
    persons are looking round for this information, you can aid them greatly.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: