那不断运行的C#代码 - 服务或单独的线程? [英] C# code that run constantly - service or separate thread?

查看:130
本文介绍了那不断运行的C#代码 - 服务或单独的线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有了相关的3个独立项目.NET 4的Web应用程序 - DAL,BAL,和UI。我使用实体框架进行数据库交互。

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.

推荐答案

您是最适合使用的 Windows服务对于始终运行的任务

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

在IIS下一个单独的线程运行的代码是不是因为IIS的可靠机制可以随意终止线程以节省服务器资源。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆