0

I am working on a website where i want to call a method every 30 minutes for example. I want that to be done in background while user is freely using the website. After a lot of research i have found the following function.

public static async Task myFunction()
{
    //Stuff Happens on the original UI thread

   await Task.Run(() => //This code runs on a new thread, control is returned to the caller on the UI thread.
   {
        while (?)
        {

        }
        //Do Stuff
   });

//Stuff Happens on the original UI thread after the loop exits.

}

I do not know i can change that so it calls a method periodically. Please help with actual code as i am new to C# and asp.net

Edit: Quartz.net was a better alternative and it worked for me

Krishneel Singh
  • 324
  • 1
  • 8
  • 16
  • 1
    Does the user have the web page open that long? How are you triggering `myFunction`? – Ron Beyer Mar 10 '18 at 01:13
  • i want to trigger it automatically or through a button @RonBeyer – Krishneel Singh Mar 10 '18 at 01:14
  • 2
    ASP.NET and IIS make no guarantees that your web-application worker process will be kept alive and will respond to registered timer callbacks. If you have a task that runs periodically you should use your webhost's "cron job" or Scheduled Task feature to invoke a new request to your web-app instead. – Dai Mar 10 '18 at 01:14
  • 1
    The code you posted is only for desktop applications. There is no "UI thread" in web-applications. – Dai Mar 10 '18 at 01:15
  • Can this be modified to work in web applications @Dai – Krishneel Singh Mar 10 '18 at 01:18
  • 1
    Have you tried Quartz.net? It is a .net based job scheduler. But do not know if it serves your purpose. But if your need is to run an isolated task which requires no user interaction, give it a try. An asp.net based implementation can be found at https://www.mikesdotnetting.com/article/254/scheduled-tasks-in-asp-net-with-quartz-net. – Priyan Perera Mar 10 '18 at 01:23
  • 1
    Yeah...Quartz.net is great...give it a try. – programmerj Mar 10 '18 at 01:26
  • Quartz.net worked perfectly for me @PriyanPerera . – Krishneel Singh Mar 10 '18 at 03:43

0 Answers0