控制台应用程序计时器每 x 分钟调用一次 Web 方法

发布于 2024-10-09 00:19:58 字数 797 浏览 0 评论 0原文

我正在 VB.Net、VS 2008 中编码。 我编写了一个控制台应用程序,它使用网站应用程序中的 2 个 Web 方法。我需要增强这个控制台应用程序,以便它连续启动 Web 方法,也许每 x 分钟(在工作时间内)启动一次,但永远不会在最后一次调用终止之前启动,其持续时间可能会有所不同,具体取决于要处理的帐户数量。

最初,我使用任务计划程序来安排应用程序,但我认为这不会阻止同时调用两个。

虽然我看过很多关于使用计时器的帖子,但我还没有找到我真正需要的。

到目前为止,我已经:

Dim aTimer As New System.Timers.Timer()
    AddHandler aTimer.Elapsed, AddressOf TriggerWebMethods

' Set the Interval to 10 minutes:
aTimer.Interval = 1000 * 60 * 10            '(1 second * 60 = 1 minute * 10 = 10 minutes)
aTimer.Enabled = True
aTimer.AutoReset = True
  1. 什么时候应该使用 Timer.ElapsedTimer.Tick
  2. Timer.EnabledTimer.Start 之间有什么区别,我应该只选择一个吗?
  3. 我希望在第一个网络方法完成后启动第二个网络方法。

我想让这尽可能简单。感谢您的所有帮助。

I am coding in VB.Net, VS 2008.
I wrote a console app that consumes 2 web methods from a web site application. I need to enhance this console app so that it launches the web methods continuously, perhaps every x minutes (during business hours), but never before the last invocation has terminated, whose duration may vary, depending on how many accounts there are to process.

Originally, I scheduled the application using Task Scheduler, but I think this doesn't prevent two invocations at the same time.

Although I have seen many posts on using timers, I haven't found exactly what I need.

So far I have:

Dim aTimer As New System.Timers.Timer()
    AddHandler aTimer.Elapsed, AddressOf TriggerWebMethods

' Set the Interval to 10 minutes:
aTimer.Interval = 1000 * 60 * 10            '(1 second * 60 = 1 minute * 10 = 10 minutes)
aTimer.Enabled = True
aTimer.AutoReset = True
  1. When should Timer.Elapsed be used vs. Timer.Tick?
  2. What is the difference between Timer.Enabled vs Timer.Start, and should I be selecting just one?
  3. I would like the 2nd web method to kick off when the first one is done.

I'd like to keep this as simple as possible. Thank you for all help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泛滥成性 2024-10-16 00:19:59

只是为了给 Jeff M 的答案补充一点。 Timer.Elapsed 事件具有以下注释。

如果 SynchronizingObject 属性为 null,则 Elapsed 事件为
在 ThreadPool 线程上引发。如果
处理已过去的事件持续
长于间隔,该事件可能
在另一个线程池上再次引发
线。在这种情况下,此次事件
处理程序应该是可重入

由于您位于控制台应用程序中,因此您可以手动滚动自己的 SynchronizingObject,也可以将 AutoReset 设置为 False,并将 TriggerWebMethods 更改为在末尾开始。您甚至可能想要偏移间隔以考虑处理时间量。

例如

    Dim start As Date = Now


    'do stuff


    Dim ts As TimeSpan = Now - start

    Dim i As Integer = (1000 * 60 * 10) - ts.TotalMilliseconds

    Dim aTimer As Timers
    aTimer.Interval = i

Just to add a little to Jeff M's answer. The Timer.Elapsed Event has the following note.

If the SynchronizingObject property is null, the Elapsed event is
raised on a ThreadPool thread. If the
processing of the Elapsed event lasts
longer than Interval, the event might
be raised again on another ThreadPool
thread. In this situation, the event
handler should be reentrant.

Since you're in a Console app you can either hand roll you own SynchronizingObject or you can set the AutoReset to False, and change your TriggerWebMethods to have a start at the end. You may even want to offset the interval to take into consideration the amount of processing time.

e.g.

    Dim start As Date = Now


    'do stuff


    Dim ts As TimeSpan = Now - start

    Dim i As Integer = (1000 * 60 * 10) - ts.TotalMilliseconds

    Dim aTimer As Timers
    aTimer.Interval = i
手心的海 2024-10-16 00:19:58

如果您正在处理 System.Timers.Timer,那么您只能使用 Elapsed 事件。如果是 System.Windows.Forms.Timer,那么您将使用 Tick 事件。您没有编写 WinForms 应用程序,因此您将使用 System.Timers.Timer。

就我个人而言,我只会使用 Enabled 属性来查看计时器是否已启动。我不会用它来启动或停止它。使用 Start()Stop() 方法可以非常清楚地了解计时器发生的情况。

如果您的 Web 方法同步执行,您只需在 TriggerWebMethods() 方法中逐个调用它们即可。在第一个完成之前不会调用第二个。

Sub TriggerWebMethods(source As Object, e As ElapsedEventArgs)
    FirstWebMethod()
    SecondWebMethod()
End Sub

如果是异步的,您必须在第一个 Web 方法上注册回调,以便在第二个 Web 方法完成时执行它。在VB中,我相信你可以直接使用第二个作为回调,具体取决于你如何进行异步调用。 (抱歉,我的 VB 现在很生疏,所以语法可能不是 100% 正确)

Sub FirstWebMethod()
    ' ...
End Sub

Sub SecondWebMethod()
    ' ...
End Sub

Sub TriggerWebMethods(source As Object, e As ElapsedEventArgs)
    Dim first As Action = AddressOf FirstWebMethod
    first.BeginInvoke(AddressOf SecondWebMethod, first)
End Sub

If you are dealing with a System.Timers.Timer, then you'd only have the Elapsed event available. If a System.Windows.Forms.Timer, then you'd use the Tick event. You're not writing a WinForms app so you would be using the System.Timers.Timer.

Personally, I would only use the Enabled property to see if the timer has been started. I wouldn't use it to start or stop it. Using the Start() or Stop() method makes it very clear what's happening to the timer.

If your web methods execute synchronously, you could just call them one after the other in your TriggerWebMethods() method. The second will not be called until the first completes.

Sub TriggerWebMethods(source As Object, e As ElapsedEventArgs)
    FirstWebMethod()
    SecondWebMethod()
End Sub

If asynchronously, you'd have to register a callback on the first web method to execute the second when it completes. In VB, I believe you can use the second directly as the callback, depending on how you make the asynchronous call. (Sorry, my VB is very rusty now so might not be 100% correct syntax)

Sub FirstWebMethod()
    ' ...
End Sub

Sub SecondWebMethod()
    ' ...
End Sub

Sub TriggerWebMethods(source As Object, e As ElapsedEventArgs)
    Dim first As Action = AddressOf FirstWebMethod
    first.BeginInvoke(AddressOf SecondWebMethod, first)
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文