如何创建定期轮询过程? [英] How to create a periodic polling process?
问题描述
我正在寻找有关如何使用最新技术创建定期轮询过程 [应用程序] 的建议/指导/示例.
I'm looking for suggestions/guidance/examples on how to create a periodic polling process [an application] using the latest technique(s).
我有一个 Web 应用程序,它根据用户操作将条目插入到队列表中.该条目代表做某事的请求.一旦条目被处理,它将从队列中删除.在处理之前,队列中可以有 1 个以上的请求.
I have a web application that inserts an entry into a queue table based upon a Users action. The entry represents a request to do something. Once the entry is processed, it will be removed from the queue. Until processed, there can be more than 1 request in the queue.
我想每 3 分钟轮询一次该队列表,每 7 分钟 24 次,然后启动一个进程"来处理请求.
I want to poll that queue table every 3 minutes, 24 by 7 and then initiate a 'process' to handle the request(s).
这个定期轮询过程 [应用程序] 应该是控制台应用程序、WCF 服务还是常规 Web 服务?我是否应该使用多线程技术来处理在给定的轮询"时间(每 3 分钟)可能在队列中的多个请求?
Should this periodic polling process [an application] be a console app, a WCF service or a regular web service? Should I use a multi-threading technique to process multiple requests that may be in the queue at a given 'poll' time (every 3 minutes)?
我在 Google 上看到过一些民意调查的例子,但它们看起来陈旧过时.有些使用线程,有些使用任务、计时器等.
I've seen a few examples of polling when I Google, but they seem old and out-dated. Some use threads, others tasks, timers, etc.
有点困惑什么是最好的方法.任何最佳实践"建议/指导/示例将不胜感激.
Somewhat confused as what is the best approach. Any "best practice" suggestions/guidance/examples would be most appreciated.
推荐答案
好吧,WCF 服务主要在您需要进行远程处理时很有用,在您的情况下,您的轮询是自主的,因此根本不需要其他任何东西比单线程.做你想做的最简单的方法是标准的 Windows 服务.在您的服务中,在无限循环中启动一个线程.在里面,输入你的代码来检查队列并处理它,然后暂停 3 分钟(Thread.Sleep(180000)).
Well, a WCF service is primarily useful when you need to do remoting, in your case your polling is autonomous, so there is no need at all for anything other than a single thread. The simplest way to do what you want would be a a standard Windows service. In your service, start a thread in an infinite loop. Inside, put in your code to check the queue and process it, and then a 3 minutes pause (Thread.Sleep(180000)).
至于处理队列时的多线程由您决定,并且不会对轮询本身产生影响.要运行的任务是否预计需要很长时间?并行性会提供明显的优势吗?取决于您是希望处理数千个请求还是每次轮询处理 5 个请求,或者要运行的任务是否可能需要 3 分钟以上,您的答案会有所不同.
As far as multi-threading when processing the queue that is up to you, and will have no effect on the polling itself. Is the task to be run expected to take a long time? Would parallelism offer a distinct advantage? Depending if you expect to process thousands of requests or five every poll, or if the task to be run might take more than 3 minutes, your answers will be different.
最简单的例子:
Private Const pollingRefreshMs = 180000
Private pollingThread As New Thread(AddressOf PollQueue) With {.IsBackground = True}
Protected Overrides Sub OnStart(ByVal args() As String)
pollingThread.Start()
End Sub
Protected Overrides Sub OnStop()
' Abort the polling thread when the service is stopped.
pollingThread.Abort()
End Sub
Private Sub PollQueue()
Dim bAborted As Boolean = False
Do
Try
' Check queue and process items here, possibly spawning a separate thread for each item...
Thread.Sleep(pollingRefreshMs) ' Pause the polling for 3 minutes.
Catch ex As ThreadAbortException
bAborted = True
End Try
Loop Until bAborted
End Sub
这篇关于如何创建定期轮询过程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!