4

I am using ASP.Net and C#. I want to synchronise something on a particular time. I made a method for doing this and it's working. But my problem is how to call this method daily at a particular time.

Client doesn't trust any third party tool, so can't use it.

Windows service is not a good solution.

If I make a web service, how should I call it at a particular time on a daily basis?

For example, I want to run method everyday at 07.00 PM.

Widor
  • 13,003
  • 7
  • 42
  • 64
Pankaj Tiwari
  • 1,378
  • 11
  • 13
  • 3
    What you're talking about is a cron job. You could have a windows schedule request your page at a specified time. – gideon May 29 '12 at 07:06
  • @gideon, windows schedule request, how? Pls keep in mind, my application is using a sharing server, so we don't have RDP and enough rights to do more – Pankaj Tiwari May 29 '12 at 07:12
  • Hm. Could you clarify what kind of third party tools your client doesn't trust? – gideon May 29 '12 at 07:15
  • Financial domain clients like AMEX doesn't trust on ANY third party tools :( They want to do every task within own environment only. – Pankaj Tiwari May 29 '12 at 07:19

2 Answers2

9

At startup, add an item to the HttpRuntime.Cache with a fixed expiration. When cache item expires, do your work, such as WebRequest or what have you. Re-add the item to the cache with a fixed expiration.

private static CacheItemRemovedCallback OnCacheRemove = null;

protected void Application_Start(object sender, EventArgs e)
{
    AddTask("DoStuff", 60);
}

private void AddTask(string name, int seconds)
{
    OnCacheRemove = new CacheItemRemovedCallback(CacheItemRemoved);
    HttpRuntime.Cache.Insert(name, seconds, null,
        DateTime.Now.AddSeconds(seconds), Cache.NoSlidingExpiration,
        CacheItemPriority.NotRemovable, OnCacheRemove);
}

public void CacheItemRemoved(string k, object v, CacheItemRemovedReason r)
{
    // do stuff here if it matches our taskname, like WebRequest
    // re-add our task so it recurs
    AddTask(k, Convert.ToInt32(v));
}
Raab
  • 34,778
  • 4
  • 50
  • 65
  • It won't affect performance, if I want to execute task after one day or more, I need to keep cache for one or more days, Isn't it? – Pankaj Tiwari May 29 '12 at 07:25
  • If Depends on How much tasks you need to cache. obviously No of task should be limited. – Raab May 29 '12 at 07:29
  • @CoDeaDDict: For Example, I want to run method everyday at 07.00 PM. Could it feasible to do through this cache expiration? – Pankaj Tiwari May 29 '12 at 10:37
  • Why not. it is simply a place where you can do anything, if resources are accessible. – Raab May 29 '12 at 10:43
  • Ok, Thanks. If you could tell me how? it would be appreciated. – Pankaj Tiwari May 29 '12 at 10:49
  • call you method o this line // do stuff here if it matches our taskname, like WebRequest – Raab May 29 '12 at 11:08
  • @CoDeaDDict: For Example, I want to run method everyday at 07.00 PM, What time should be written in cache expiration, to call it daily on 07.00 PM? – Pankaj Tiwari May 29 '12 at 12:11
  • calculate your time in seconds from current time to 07:00 PM and pass. – Raab May 29 '12 at 12:21
  • Hi Rab Nawaz/Pankaj, I have to do similar type task in my shared server where my application is host and perform some task everyday so i follows your given step,it does not work after few hours.Have you crack this situation especially pankaj how to achieve your requirement plz let me know. – Neeraj Dubey Apr 07 '14 at 07:07
0

You can use windows scheduled task to run the application (exe) or create windows service and use timer. Take a look at Quartz.NET it also can used.

Community
  • 1
  • 1
Damith
  • 62,401
  • 13
  • 102
  • 153