1

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.

Infinite Recursion
  • 6,511
  • 28
  • 39
  • 51
DeveloperM
  • 1,129
  • 7
  • 17
  • 30
  • What does your `TriggerWebMethods()` method look like? – Jeff Mercado Dec 22 '10 at 21:08
  • The whole process is fairly complex: data is being transferred from the db on one server to the db on another, encrypted, and then, asynchronously, transferred to a destination server, where it is decrypted. New data will become available for transmission throughout the day. So TriggerWebMethods contains a call to each web reference, followed by logging the time to the Event Log. – DeveloperM Dec 22 '10 at 21:17
  • System.Timers.Timer has a Elapsed-Event and System.Windows.Forms.Timer has a Tick-Event. Have a look at this link to see the differences: http://www.go4expert.com/forums/showthread.php?t=2122 – Tim Schmelter Dec 22 '10 at 21:24
  • Any reason why you're not doing this as a Windows Service? – Conrad Frix Dec 22 '10 at 21:52
  • @Conrad: No reason re Windows Service. I only first heard of Windows Services yesterday when I started researching this, so I don't know much about them. – DeveloperM Dec 23 '10 at 14:32
  • Well if you had you did use windows events you get a well understood way (by the user) of running an application in the background that has a lot of support. Using a console app is a bit easier to write and run but you'll be missing all of the other support management options (too many options to list in a comment) – Conrad Frix Dec 24 '10 at 02:02

2 Answers2

1

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
Jeff Mercado
  • 129,526
  • 32
  • 251
  • 272
  • Thanks, Jeff! What's a Callback? If it's too complicated to explain here, can you provide a link? The code I currently have does not seem to call the web methods at all. I have a breakpoint on TriggerWebMethods but the execution doesn't get there. – DeveloperM Dec 22 '10 at 21:51
  • @Developer: A callback is just some method to be called when the asynchronous call completes. In your case, when the first method call completes, you want to call the second. – Jeff Mercado Dec 22 '10 at 22:25
  • All: I need some more help. My web method is not being called, as far as I can see. I have the web app launched from VS, and I have a breakpoint established on the entry point of the first web method. I launch the console app (also from the IDE) and watch it execute a few lines of code and then apparently stop. Do and of you guys have a few more minutes to help? Thank you. – DeveloperM Dec 23 '10 at 17:03
  • @Developer: Could you update your question to include how you call your web methods and mark where you say it stops? We'd need a bit more information. – Jeff Mercado Dec 24 '10 at 00:05
  • +1 Nice answer. Would've done it earlier but I was out of votes (stupid electorate badge and its 25% question requirement.) – Conrad Frix Dec 24 '10 at 01:55
0

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
Conrad Frix
  • 51,984
  • 12
  • 96
  • 155
  • Conrad and Jeff, thank you for taking the time to respond. But I seem to be going backwards. I can't get the web method call to trigger at all now. I have seen some examples where someone coded WithEvents when instantiating the Timer. What's the difference in these 2 approaches? – DeveloperM Dec 23 '10 at 17:30
  • Take a look at http://stackoverflow.com/q/2208775/119477 for the WithEvents bit. Not sure why your event isn't being triggered. What does your AddHandler look like? – Conrad Frix Dec 24 '10 at 01:59