简体   繁体   English

如何在ASP.NET中动态运行进程

[英]How to dynamically run process in asp.net

I am designing a website and it uses Windows Forms (in Visual Studio 10) in which for example i have five-six URLs . 我正在设计一个网站,它使用Windows Forms(在Visual Studio 10中), 例如,我有5-6个URL Now i am displaying them on home page of my website xyz.com 现在,我将它们显示在我的网站xyz.com的主页上

What i want is, i want to calculate total no. 我想要的是,我想计算总数。 of tweets for all links and display links based on no. 所有链接的推文数量,并根据否显示链接。 of times they are being tweeted/retweeted. 他们被推文/转发的次数。

for a url we can calculate no. 对于网址,我们可以计算出否。 of tweet using twitter api http://urls.api.twitter.com/1/urls/count.json?url=YourURL Twitter API http://urls.api.twitter.com/1/urls/count.json的tweet的URL = YourURL

I know all the stuff like receiving JSON values in a string and parsing json to retrieve tweet counts and then compare and display links based on the priority etc. 我知道所有东西,例如在字符串中接收JSON值并解析json以检索推文计数,然后根据优先级比较和显示链接等。

What i have been using till now it is initiating all the process using a Click_Button. 到目前为止,我一直在使用Click_Button来启动所有过程。

But i want to know how can i automate this all for each 10 minutes. 但是我想知道如何每10分钟自动完成一次。 Its like a end user can see urls priority with just refreshing the page. 就像最终用户可以通过刷新页面看到url优先级一样。

One way to do this is to run a scheduled task ever 10 mins which interacts with the DB. 一种方法是每10分钟运行一次计划任务,该任务与数据库交互。 The web application also interacts with the DB and thus the two systems are distinct. Web应用程序还与DB交互,因此这两个系统是不同的。

Side note: it is strongly recommended to use only console applications as scheduled tasks. 旁注:强烈建议仅将控制台应用程序用作计划任务。 If you make a windows form application will will have some issues. 如果您制作Windows窗体应用程序,将会遇到一些问题。


As Kieren Johnstone has pointed out in another answer the best way to do this would be to write a windows service. 正如Kieren Johnstone在另一个答案中指出的那样,做到这一点的最佳方法是编写Windows服务。

I still recommend the solution as described above as a first step since it is easy to debug and test. 我仍然建议将上述解决方案作为第一步,因为它易于调试和测试。

Additionally, give some serious consideration to logging and error reporting -- with background tasks you can never know to much about what the heck it was doing when it broke. 此外,请认真考虑日志记录和错误报告-使用后台任务,您可能永远不知道它崩溃后到底在做什么。

If timing itself is not important (it doesn't have to be 10 minutes precisely), I would suggest binding to any event that fires when users use your application. 如果计时本身并不重要(不必精确到10分钟),那么我建议您绑定到用户使用您的应用程序时触发的任何事件。 No point in calculating anything if noone is using it :-) 如果没有人使用它,那么计算任何东西都没有意义:-)

So you could use a login, or page load, or whatever happens at an interval roughly like the interval you wish to achieve. 因此,您可以使用登录名或页面加载,或在与您希望达到的间隔大致相同的间隔内进行的任何操作。

You can always store a DateTime variable somewhere that you can check to see when the calculation was last made. 您始终可以将DateTime变量存储在可以检查以查看上次计算时间的位置。 Something like: 就像是:

public void MyEventHasFired()
{
     DateTime dateLastProcessed = ... //Database? Session data? Anything goes.
     if(dateLastProcessed < DateTime.Now.AddMinutes(-10))
     {
          //calculate

          ...

          dateLastProcessed = DateTime.Now;
     }
}

The best solution is definitely a Windows Service. 最好的解决方案肯定是Windows服务。 It can be started, stopped and managed well, it's easy to log, maintain.. 它可以很好地启动,停止和管理,易于记录,维护。

Scheduled Tasks are very prone to problems. 计划任务非常容易出现问题。 At least in a Windows Service you can configure it to start automatically, re-start if there's a problem, you can control the timing yourself in the code, and catch/handle exceptions as you wish. 至少在Windows服务中,您可以将其配置为自动启动,如果出现问题可以重新启动,可以在代码中自行控制时间,并根据需要捕获/处理异常。

The best scheduler i know is Quartz.net 我知道的最好的调度程序是Quartz.net

It'is not simple to use but it works great. 使用起来并不简单,但是效果很好。

You can find an example with asp.net there http://blogs.planetcloud.co.uk/mygreatdiscovery/post/ASPNET-Scheduled-Tasks-with-QuartzNET.aspx 您可以在http://blogs.planetcloud.co.uk/mygreatdiscovery/post/ASPNET-Scheduled-Tasks-with-QuartzNET.aspx中找到使用asp.net的示例。

Anyway i agree with Kieren Johnstone: you should use a windows service 无论如何,我同意Kieren Johnstone:您应该使用Windows服务

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM