Scheduled jobs in ASP.NET website without buying dedicated servers

How can I perform various task (such as email alert/sending news letter) on a configured schedule time on a shared hosting server?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Here’s a Global.ascx.cs file I’ve used to do this sort of thing in the past, using cache expiry to trigger the scheduled task:

public class Global : HttpApplication
{
    private const string CACHE_ENTRY_KEY = "ServiceMimicCacheEntry";
    private const string CACHE_KEY = "ServiceMimicCache";

    private void Application_Start(object sender, EventArgs e)
    {
        Application[CACHE_KEY] = HttpContext.Current.Cache;
        RegisterCacheEntry();
    }

    private void RegisterCacheEntry()
    {
        Cache cache = (Cache)Application[CACHE_KEY];
        if (cache[CACHE_ENTRY_KEY] != null) return;
        cache.Add(CACHE_ENTRY_KEY, CACHE_ENTRY_KEY, null,
                  DateTime.MaxValue, TimeSpan.FromSeconds(120), CacheItemPriority.Normal,
                  new CacheItemRemovedCallback(CacheItemRemoved));
    }

    private void SpawnServiceActions()
    {
        ThreadStart threadStart = new ThreadStart(DoServiceActions);
        Thread thread = new Thread(threadStart);
        thread.Start();
    }

    private void DoServiceActions()
    {
        // do your scheduled stuff
    }

    private void CacheItemRemoved(string key, object value, CacheItemRemovedReason reason)
    {
        SpawnServiceActions();
        RegisterCacheEntry();
    }
}

At the moment, this fires off your actions every 2 minutes, but this is configurable in the code.

Method 2

Somone on here was doing this by creating threads in global.asax. Sounded like they were having success with it. I’ve never tested this approach myself.

This in my opinion would be a better option then overloading the cache expiration mechanism.

Method 3

You can use ATrigger scheduling service on a shared hosting without any problem.
A .Net library is also available to create scheduled tasks without overhead.

Disclaimer: I was among the ATrigger team. It’s a freeware and I have not any commercial purpose.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x