Solved

How to Schedule job?

Posted on 2002-07-15
6
196 Views
Last Modified: 2010-05-02
I have a migration process that I need to schedule in Vb.
e.g. I want to kick it on at 7.00 pm and it should end at
5.00 am in the morning automatically.  The form has two text boxes named - Start Time and End Time.
How can I achieve this through Timer control?
Any help will be greatly appreciated.

Thanks,
0
Comment
Question by:khatrij
6 Comments
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 7155559
Maintain your open questions and you may find Experts more receptive:

Questions Asked 15
Last 10 Grades Given A A A B C C C C B  
Question Grading Record 9 Answers Graded / 9 Answers Received

Thanks,
Anthony
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7155651
Timer control is not too acurate to this type of job. Instead of it, use API:
' you could set the interval to hours which there is not a straigth way to do it with timer control (need creates some code to get same results with the control).
' from MSDN

HOWTO: Create a Timer Event Using the Windows API Functions
Last reviewed: February 18, 1998
Article ID: Q180736  
The information in this article applies to:
Microsoft Visual Basic Learning, Professional, and Enterprise Editions for Windows, version 5.0


SUMMARY
This article shows you how to create a timer event using the SetTimer and KillTimer Windows API functions. The article also includes a sample project that demonstrates how to use these functions.

You can use these functions if you need to execute events at intervals greater than what the Visual Basic Timer control allows. The Timer control allows a maximum interval of slightly over a minute while these API functions allow you to set an interval up to 24.86 days.



MORE INFORMATION
The SetTimer function creates a timer that executes a function at the specified time-out value. This function requires the following parameters:

hWnd identifies the window to be associated with the timer. This window is owned by the calling thread. If this parameter is NULL, no window is associated with the timer and the nIDEvent parameter is ignored.
nIDEvent specifies a nonzero timer identifier. If the hWnd parameter is NULL, this parameter is ignored.
uElapse specifies the time-out value, in milliseconds. You can use a Long data type with a value of up to 2,147,483,647 milliseconds. Values beyond this limit result in a Run-time Error '6' Overflow.
NOTE: Visual Basic adds a pound sign (#) to the end of the time value if the value exceeds this limit.

lpTimerFunc points to the function to be notified when the time-out value elapses. Use the AddressOf operator to return a pointer to the function. The callback function receives the hWnd, the NIDEvent, the UElapse, and lpTimerFunc parameters from the AddressOf function.

If the SetTimer function is successful, the function returns an integer that identifies the new timer. The KillTimer function requires this integer to destroy the timer. If the SetTimer function fails, the function returns zero.
After creating a timer, you must destroy the timer using the KillTimer function. The KillTimer function requires the following parameters:

hWnd identifies the window associated with the specified timer. This value must be the same as the hWnd value passed to the SetTimer function that created the timer.
uIDEvent specifies the timer to be destroyed. If the window handle passed to SetTimer is valid, this parameter must be the same as the uIDEvent value passed to SetTimer. If the application calls SetTimer with hWnd set to NULL, this parameter must be the timer identifier returned by SetTimer.

The KillTimer function returns a non-zero value if the function successfully destroys the timer.
The next section shows how to create a sample project that demonstrates how to use these functions in Visual Basic.



To Create the Sample Project

Start a new Standard EXE project in Visual Basic. Form1 is created by default.

Add a TextBox and CommandButton to Form1.

Add a module to the project by completing the following steps:
- From the Project menu, click Add Module. The Add Module dialog box


     appears.

- On the New tab, choose Module and click OK. A new module is added to


     your project.


Copy the following code to the Code window of Module1:
Sample Code -----------



      Option Explicit



      Declare Function SetTimer Lib "user32" _
            (ByVal hwnd As Long, _
            ByVal nIDEvent As Long, _
            ByVal uElapse As Long, _
            ByVal lpTimerFunc As Long) As Long

      Declare Function KillTimer Lib "user32" _
            (ByVal hwnd As Long, _
            ByVal nIDEvent As Long) As Long

      Global iCounter As Integer

      Sub TimerProc(ByVal hwnd As Long, _
                     ByVal uMsg As Long, _
                     ByVal idEvent As Long, _
                     ByVal dwTime As Long)

          iCounter = iCounter + 1
          Form1.Text1.Text = CStr(iCounter)
      End Sub



Copy the following code to the Code window of Form1:
Sample Code -----------



      Option Explicit
      Dim lngTimerID As Long
      Dim BlnTimer As Boolean



      Private Sub Form_Load()
         BlnTimer = False
         Command1.Caption = "Start Timer"
      End Sub

      Private Sub Command1_Click()
      'Starts and stops the timer.

         If BlnTimer = False Then
            lngTimerID = SetTimer(0, 0, 200, AddressOf TimerProc)
            If lngTimerID = 0 Then
              MsgBox "Timer not created. Ending Program"
              Exit Sub
            End If
            BlnTimer = True
            Command1.Caption = "Stop Timer"
         Else
            lngTimerID = KillTimer(0, lngTimerID)
            If lngTimerID = 0 Then
               MsgBox "couldn't kill the timer"
            End If
            BlnTimer = False
            Command1.Caption = "Start Timer"
          End If

      End Sub



On the Run menu, click Start or press the F5 key to start the program. Click Start Timer to create a timer event. At each specified interval, the TextBox is updated with a new value. Click Stop Timer to stop the timer event.

REFERENCES
For additional information about the SetTimer and KillTimer functions, refer to the Platform SDK Product Documentation.

For additional information about the AddressOf operator, refer to the Visual Basic Help.
 
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7155668
if math doesn't joke with me, 3600000= 1 hour so:
Use DateDiff function with "h" as interval parameter to set the interval time and multiply with 3600000.
Hope it helps
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 22

Expert Comment

by:rspahitz
ID: 7155721
There was a similar question last week and one excellent choice was to use the Windows 2000 scheduler, but that would be done by the user, not a VB app.

To do this through an app, you can add a timer that goes off every second and checks the system clock against the starting/ending time.  If the clock exceeds the start time and the app is not running, start the app.  I'm not sure what kind of app you have that doesn't know when it's done, but you could probably have it check the stop time and then terminate the app.
0
 

Author Comment

by:khatrij
ID: 7155803
Hi rspahitz,

I have to call a procedure named MigrateDocuments()
I am not sure how do you compare the system time. Can you please give me some idea?

Thanks
0
 
LVL 22

Accepted Solution

by:
rspahitz earned 100 total points
ID: 7155812
Just use the timer function. The code below will count five seconds:

  Dim tmr As Date
  tmr = Timer + 5
  While tmr > Timer
    Label1.Caption = Timer
    DoEvents
  Wend

Your code would be something like this:

private sub Timer1_Timer()
  if Timer < cdate(txtStartTime.text)
    ' waiting for start time
  elseif Timer1 < cdate(txtEndingTime.text)
    if not boolStarted then
      Timer1.enabled=false ' temporarily disable timer
      boolStarted =true
      PerformStartUpProcedure ' or maybe launch an app
      Timer1.enabled=true ' re-enable timer
    endif
  else
    ' end time exceeded
    Timer1.enabled=false ' temporarily disable timer
    PerformWrapUpProcedure ' maybe terminate the launched app
    Timer1.enabled=true ' re-enable timer, or maybe not...
  end if
end sub

To start, make sure the Timer control is set to 1000 milliseconds (or anyother reasonable time to wait between checks) and set enabled=false until you decide to start the check, which is probably after you validate the start and end times.

0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question