Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# code that run constantly - service or separate thread?

I have a .NET 4 web application that has 3 separate projects associated – DAL, BAL, and UI. I am using Entity Framework for database interaction.

I have code that cycles through a bunch of database data, calls methods depending on what it finds, and then updates a database. I want this code to run all the time. At the same time I want users to be able to log in and run reports etc all while the code in the background is constantly running.

What is a good approach to this? Do I create a service for the code that constantly runs, a separate thread, an entirely separate project for the code that runs constantly, or a different approach..?

Also, depending on the answers given, how would I kick start the code that runs constantly? i.e. just through form load method or is there a better approach? I currently kick start the code by clicking a start button; this is fine for testing, but not going to work when in production.

like image 930
user1595656 Avatar asked Aug 13 '12 14:08

user1595656


1 Answers

You would be best suited for using Windows Services for always-running tasks.

Running code on a separate thread under IIS is not a reliable mechanism since IIS can terminate threads at will to conserve server resources.

like image 200
SliverNinja - MSFT Avatar answered Oct 06 '22 01:10

SliverNinja - MSFT