运行不断的C#代码 - 服务或单独的线程? [英] C# code that run constantly - service or separate thread?
问题描述
我有一些代码循环遍历一堆数据库数据,调用方法取决于找到的内容,然后更新数据库。我希望这段代码能够一直运行。同时,我希望用户能够登录并运行报告等,而后台的代码则不断运行。
这是一个很好的方法?我是否为不断运行的代码创建一个服务,一个单独的线程,一个完全独立的代码运行不间断的项目,或者一个不同的方法..?
另外根据给出的答案,我如何启动不断运行的代码?即只是通过表单加载方法还是有更好的方法?我现在通过点击开始按钮启动代码;这是很好的测试,但不会在生产中工作。
你最适合使用 Windows服务 for 始终运行的任务
在IIS下的单独线程上运行代码不是可靠的机制,因为IIS可以随意终止线程以节省服务器资源。
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.
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.
这篇关于运行不断的C#代码 - 服务或单独的线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!