0

I have a function that takes about 2-3 minutes to do all of the processing it needs

It only needs to do the calculations once daily. The processing is done inside of a background thread that is started inside Application_Start in Global.asax.cs.

I am worried that the application will kill the method while it is running.

dolphone bubleine
  • 691
  • 1
  • 8
  • 18

2 Answers2

2

You are doing the process in in Application_Start, are you restarting your application daily ?. A better approach would be to create a separate executable for the task, and schedule it on your server using Windows Scheduler.

Habib
  • 219,104
  • 29
  • 407
  • 436
  • 1
    +1 also, "What happens if your application is restarted unexpectedly more than once daily? Hopefully that's OK." – Chris Shain Aug 06 '12 at 05:10
  • It's for a stock bot. The method tries to execute starting at 2AM. If it fails or stocks have not been updated it waits an hour and tries again. The method in question just analyzes all of the stocks. If it runs more than once that is not a problem. The app will be hosted on a WinHost server (not my computer) so I don't think a .exe is an option. – dolphone bubleine Aug 06 '12 at 05:15
  • @user1526247, how are you planning to schedule the function to execute at 2AM, [Application_Start](http://msdn.microsoft.com/en-us/library/ms178473.aspx) is called only one time during the life cycle of an application. – Habib Aug 06 '12 at 05:23
  • in bg thread - while(true) if after 2am try stuff - if success or tried n times setDbFlag to work done for today. if fail Thread.Sleep for an hour – dolphone bubleine Aug 06 '12 at 05:26
  • @user1526247, since you don't have access to task scheduler, your current work around seems the only possible approach. You may wanna see this thread http://stackoverflow.com/questions/655012/what-are-some-best-practices-for-managing-background-threads-in-iis, also I don't think the application will kill this method, but you have to give it a shot to see yourself. – Habib Aug 06 '12 at 05:33
1

For the above scenario, either you can use windows service or console application and schedule it when you want it to run.

But doing it at the start of application is never recommended in this scenario.

Narendra
  • 3,069
  • 7
  • 30
  • 51