Scheduled Tasks on ASP.NET without Purchasing Dedicated Servers

How can I perform a different task (e.g. email notification / newsletter sending) at a configured schedule time on a shared hosting server?

+3
source share
3 answers

Here the Global.ascx.cs file that I used before used the cache code to run 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();
    }
}

Currently it works every 2 minutes, but it is configured in the code.

+10
source

Somone did this here by creating threads in global.asax. It seems that they have succeeded. I have never tested this approach myself.

, , , .

0

You can use the ATrigger distribution service on a shared hosting without any problems . The .NET library is also available for creating scheduled tasks without overhead.

Disclaimer: I was among the Atrigger team. This is a free program and I have no commercial purpose.

0
source

Source: https://habr.com/ru/post/1706172/


All Articles