Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi...friends

i am write code in global.asax i want to run code every day once at time when server time is 24:00:00 or(12 pm) so which condition i pass in Application_Start???

how to perform this task using global.asax?

my website allready hosted on server.
Posted
Updated 28-Feb-12 0:29am
v3

@bhavesh002 You can do it in asp.net Global.asax

I am providing here code by using which you can perform your desired action
using this schedule class

public class Scheduler
 {
     private class CacheItem
     {
         public string Name;
         public Callback Callback;
         public Cache Cache;
         public DateTime LastRun;
     }


     public delegate void Callback();

     private static int _numberOfMinutes = 1;

     public static void Run(string name, int minutes, Callback callbackMethod)
     {
         _numberOfMinutes = minutes;

         CacheItem cache = new CacheItem();
         cache.Name = name;
         cache.Callback = callbackMethod;
         cache.Cache = HttpRuntime.Cache;
         cache.LastRun = DateTime.Now;
         AddCacheObject(cache);
     }


     private static void AddCacheObject(CacheItem cache)
     {
         if (cache.Cache[cache.Name] == null)
         {
             cache.Cache.Add(cache.Name, cache, null,
                  DateTime.Now.AddMinutes(_numberOfMinutes), Cache.NoSlidingExpiration,
                  CacheItemPriority.NotRemovable, CacheCallback);
         }
     }


     private static void CacheCallback(string key, object value, CacheItemRemovedReason reason)
     {
         CacheItem obj_cache = (CacheItem)value;
         if (obj_cache.LastRun < DateTime.Now)
         {
             if (obj_cache.Callback != null)
             {
                 obj_cache.Callback.Invoke();
             }
             obj_cache.LastRun = DateTime.Now;
         }
         AddCacheObject(obj_cache);
     }

 }


Here you should write your code like this in global.asax

C#
protected void Application_Start(object sender, EventArgs e)
       {
           if(DateTime.Now.TimeOfDay == Yourtime;)
           Scheduler.Run("test", 20, RunScheduleTasks);
       }


Mark as answer if it helps
 
Share this answer
 
Application_Start exists for a different purpose. You cannot make it run a scheduled job. They are various ways to schedule jobs. If it is a database-centric job, your database must have a mechanism to schedule jobs. (I know SQL Server has one). You could also use Windows Server Task scheduler or any other 3rd party scheduler.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900