vbNewbie
vbNewbie

Reputation: 3345

scheduling tasks in vb.net

I have an application that I need to automate where at a certain time each day, say 6pm, it will run a certain method(which is to check a database for key terms and then access the api to search for these terms). But there is also another process which is running all the time accessing the stream api so when the search is complete, it interrupts the stream and hands over the new terms. Now I thought of adding the .exe file to windows task scheduler but not sure if that would work. The stream method run indefinitely and at 6pm each day another process needs to run. I thought of using the system.threading.task TaskFactory but when I include it, it shows taskfactory as undefined. ( I do have .net framework 4.0)If it launches using the task scheduler as 6pm with my code logic as follows:

        While True
            'should i run this method as background 
            dim gStream as StreamProcess
            gStream.module1()


            If DateTime.Now.TimeOfDay.ToString = searchTime Then
                addKeywordSearchRules = getSTerms.getNewTerms(addKeywordSearchRules)
                ruleManip.ruleChanges(ruleMethods.Method.ADD, addKeywordSearchRules)
            End If

            Dim gSearch As SearchProcess
            if not addKeywordSearchRules.count = 0 then
               gSearch.module1()
            end if 

        End While

Does this logic make sense? Any other ideas>

Upvotes: 0

Views: 4197

Answers (1)

Tracer
Tracer

Reputation: 172

An alternative would be to use a system.windows.forms.timer object.

  1. Place the timer object on your form
  2. Set the "Enabled" property to True
  3. Set the "Interval" property to 24hrs, i.e (24 * 60 * 60 * 1000, because the unit is milliseconds)
  4. In the "tick" event of the Timer object, set it to run whatever function you want to be executed every 24 hours.

i.e do something like the following, assuming your Timer object is called Timer1:

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    'run your desired function here
End Sub

Hope that helps.

EDIT: The following is sample code to make this work in a console application...

Imports System.Windows.Forms
Module Module1
    Public WithEvents timer As New System.Windows.Forms.Timer
    Sub Main()
        timer.Enabled = True
        timer.Interval = 500 'replace 500 milliseconds with 24 hours'
        MsgBox("pause")
    End Sub

    Private Sub Timer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timer.Tick
        'run your desired method here'
    End Sub

End Module

The minor differences are:

  1. You have to explicitly import System.Windows.Forms
  2. Within your module, you have to declare the timer as WithEvents, so that your module gets notified of the "Tick" event

You can import system.windows.forms by first double-clicking on "My project" in your project explorer, then going to "References", then clicking on "Add", then choosing the ".Net" tab and choosing System.windows.forms. I hope those are the right english names, because I am using visual studio in a different language.

Upvotes: 2

Related Questions