Results 1 to 30 of 30

Thread: [RESOLVED] Creating a Delay of 60 min

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Posts
    507

    Resolved [RESOLVED] Creating a Delay of 60 min

    Hi

    I am trying to create a function that will delay calling an EXE file for 60 minutes. Example:

    The user will get a dialogue window that will prompt him to make a choice, if chooses the install he will click on botton that will delay exeuting the EXE for specific number if min. and after 5 delay another EXE will start automaticaly.

    any suggestions will be appriciated. Iam using VB 6.

    Thanks

    Georges

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Creating a Delay of 60 min

    One way is to use timer and increment some counter within the timer:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.     Timer1.Interval = 100   'fire timer
    5.     Timer1.Enabled = True
    6.     Timer1.Interval = 60000 '1 minute
    7. End Sub
    8.  
    9. Private Sub Timer1_Timer()
    10. Static iCounter As Integer
    11.  
    12.     If iCounter = 60 Then
    13.         'do something
    14.         iCounter = 0
    15.     End If
    16.     iCounter = iCounter + 1
    17.  
    18. End Sub

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Posts
    507

    Re: Creating a Delay of 60 min

    Thank,, Why am I getting Error in "Private Sub Form_Load()" in Yellow, and it have TIMER1 in blue? The error Compile error Varialble not defined?

    thanks

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Creating a Delay of 60 min

    Do you have a Timer control on your form?

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Posts
    507

    Re: Creating a Delay of 60 min

    I am sorry, I am new VB I don't know much about it, what do you mean by Timer Control?
    thanks

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Creating a Delay of 60 min

    Quote Originally Posted by wajdi
    I am sorry, I am new VB I don't know much about it, what do you mean by Timer Control?
    thanks
    On your VB toolbar you will see a control that looks like a stopwatch. That is the Timer control.

    You need to place one of those on your form (it is invisible at run time so it doesn't matter where), and put RhinoBull's timer code in its Timer event.

  7. #7
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Creating a Delay of 60 min

    Look in your IDE's Toolbox which is usually on the left of your screen. Do you see something that looks like a stopwatch? If so then double-click it and it will appear on your form.

  8. #8
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: Creating a Delay of 60 min

    This is another method (probobally (maybe) more accurate) . Just add a timer on your form:

    VB Code:
    1. Option Explicit
    2.     Private Declare Function timeGetTime Lib "winmm.dll" () As Long
    3.     Public tmpTime As String
    4.     Public endTime As String
    5.  
    6. Private Sub Form_Load()
    7.     Dim startTime As String
    8.     Dim minutesToDelay As String
    9.         With Timer1
    10.             .Interval = 1
    11.             .Enabled = True
    12.         End With
    13.             startTime = CStr(timeGetTime)
    14.             minutesToDelay = "60" 'must be a number character, otherwise Val returns 0
    15.             endTime = Val(startTime) + Val(minutesToDelay * 60000)
    16. End Sub
    17.  
    18. Private Sub Timer1_Timer()
    19.     tmpTime = Val(timeGetTime)
    20.         If tmpTime = endTime Then Shell "Notepad.exe", vbNormalFocus
    21. End Sub

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Posts
    507

    Re: Creating a Delay of 60 min

    Perfect now I am not getting anymore errors, one more qtion, how can I set it in a way after the user tries three time delaying 60 min, it till trigger a specific EXE file?

    Thanks

  10. #10
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Creating a Delay of 60 min

    Quote Originally Posted by wajdi
    Perfect now I am not getting anymore errors, one more qtion, how can I set it in a way after the user tries three time delaying 60 min, it till trigger a specific EXE file?

    Thanks
    Set up a counter that increments by 1 each time it goes through a 60 minute delay. Each time the delay starts, check the counter. If it equals 3 then trigger the .Exe....

    You are talking about a potential 3 hour delay. What if, during that time, I decide to shut my machine off and go home? What happens to your program then?

  11. #11
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: Creating a Delay of 60 min

    Add another String. That string can be used in combination with the Shell command:

    VB Code:
    1. Dim delayCount As Integer
    2. Dim myApp As String
    3.  
    4.     myApp = "C:\myApp.exe"
    5.  
    6.         'then in Timer1, when delay reaches 3
    7.             If tmpTime = endTime And delayCount = 3 Then Shell myApp, vbNormalFocus

  12. #12

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Posts
    507

    Re: Creating a Delay of 60 min

    It should kill the program, the whole idea of what I am trying to do it force a reboot after few delays, After the user installs the upgrade that we push on their desktops, I want the user to have the choice to delay the reboot three times, each time is a wait of 60min, after the 60 min pass the same window will come up and give the user to reboot or Delay Reboot 60 min, after three delay his computer will reboot. So if he reboot his machine somewhere in the middle of the delays, I am Happy . I program should exit

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Posts
    507

    Re: Creating a Delay of 60 min

    This myApp = "c:\Windows\system32\notepad.exe" keeps giving me errors it does matter what path i enter there.

  15. #15
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Creating a Delay of 60 min

    Quote Originally Posted by wajdi
    This myApp = "c:\Windows\system32\notepad.exe" keeps giving me errors it does matter what path i enter there.
    What is the error?

  16. #16

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Posts
    507

    Re: Creating a Delay of 60 min

    Invalid outside procedure

  17. #17

  18. #18

  19. #19
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: Creating a Delay of 60 min

    Marty is right. Invalid Outside Procedure is every procedure that is in "Declarations" and is not a declaration.

  20. #20

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Posts
    507

    Re: Creating a Delay of 60 min

    Option Explicit
    Private Declare Function timeGetTime Lib "winmm.dll" () As Long
    Public tmpTime As String
    Public endTime As String

    Private Sub Form_Load()
    Dim startTime As String
    Dim minutesToDelay As String
    With Timer1
    .Interval = 1
    .Enabled = True
    End With
    startTime = CStr(timeGetTime)
    minutesToDelay = "60" 'must be a number character, otherwise Val returns 0
    endTime = Val(startTime) + Val(minutesToDelay * 60000)

    Dim delayCount As Integer
    Dim myApp As String

    myApp = "C:\windows\system32\notepad.exe"

    'then in Timer1, when delay reaches 3
    If tmpTime = endTime And delayCount = 3 Then Shell myApp, vbNormalFocus
    End Sub

    Private Sub Timer1_Timer()
    tmpTime = Val(timeGetTime)
    If tmpTime = endTime Then Shell "Notepad.exe", vbNormalFocus
    End Sub

  21. #21
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: Creating a Delay of 60 min

    VB Code:
    1. myApp = "C:\windows\system32\notepad.exe"
    ... has to be in a procedure. In your case, it can be only in Form_Load.

  22. #22
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Creating a Delay of 60 min

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.     Timer1.Interval = 100   'fire timer
    5.     Timer1.Enabled = True
    6.     Timer1.Interval = 60000 '1 minute
    7. End Sub
    8.  
    9. Private Sub Timer1_Timer()
    10. Static iCounter As Integer
    11.  
    12.     If iCounter = 60 Then
    13.         Shell "Notepad.exe", vbNormalFocus
    14.         iCounter = 0
    15.     End If
    16.     iCounter = iCounter + 1
    17.  
    18. End Sub
    You can make your code easier to read if you surround the code with VBCode tags as in this example.

    [vbcode]
    'Your code
    [/vbcode]

    which will cause it to show up like this.
    VB Code:
    1. ' Your code

  23. #23

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Posts
    507

    Re: Creating a Delay of 60 min

    VB Code:
    1. Option Explicit
    2.     Private Declare Function timeGetTime Lib "winmm.dll" () As Long
    3.     Public tmpTime As String
    4.     Public endTime As String
    5.  
    6. Private Sub Command1_Click()
    7. Dim startTime As String
    8.     Dim minutesToDelay As String
    9.         With Timer1
    10.             .Interval = 1
    11.             .Enabled = True
    12.         End With
    13.             startTime = CStr(timeGetTime)
    14.             minutesToDelay = "60" 'must be a number character, otherwise Val returns 0
    15.             endTime = Val(startTime) + Val(minutesToDelay * 60000)
    16.            
    17.             Dim delayCount As Integer
    18. Dim myApp As String
    19.  
    20.    myApp = "C:\windows\system32\shutdown.exe"
    21.  
    22.         'then in Timer1, when delay reaches 3
    23.             If tmpTime = endTime And delayCount = 3 Then Shell myApp, vbNormalFocus
    24. End Sub
    25.  
    26. Private Sub Command2_Click()
    27. Shell "c:\windows\system32\shutdown.exe"
    28. End Sub
    29.  
    30.  
    31.  
    32. Private Sub Timer1_Timer()
    33.     tmpTime = Val(timeGetTime)
    34.         If tmpTime = endTime Then Shell "Notepad.exe", vbNormalFocus
    35. End Sub

  24. #24
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: Creating a Delay of 60 min

    VB Code:
    1. Option Explicit
    2.     Private Declare Function timeGetTime Lib "winmm.dll" () As Long
    3.     Public tmpTime As String
    4.     Public endTime As String
    5. [B]    Public myApp As String[/B]
    6. [B]    Public delayCount As Integer[/B]
    7.  
    8. Private Sub Command1_Click()
    9.     Dim startTime As String
    10.     Dim minutesToDelay As String
    11.         With Timer1
    12.             .Interval = 1
    13.             .Enabled = True
    14.         End With
    15.             [B]myApp = "C:\windows\system32\shutdown.exe"[/B]
    16.             startTime = CStr(timeGetTime)
    17.             minutesToDelay = "60" 'must be a number character, otherwise Val returns 0
    18.             endTime = Val(startTime) + Val(minutesToDelay * 60000)
    19. End Sub
    20.  
    21. Private Sub Command2_Click()
    22. [B]    Shell "c:\windows\system32\shutdown.exe"[/B] '???
    23. End Sub
    24.  
    25. Private Sub Timer1_Timer()
    26.     tmpTime = Val(timeGetTime)
    27.         If tmpTime = endTime Then Shell "Notepad.exe", vbNormalFocus
    28.         [B]If tmpTime = endTime And delayCount = 3 Then Shell myApp, vbNormalFocus[/B]
    29. End Sub
    This should do it. You were declaring "myApp" in Form_Load and reffering to it in Command2_Click. If you want to reffer to some variable in general (more procedures) it must be declared in "Declaration", that's ouside of all procedures. BTW... that won't work. You're not increasing the delayCount. When every hour goes by, increase it for 1. Otherwise, this won't work.

  25. #25

  26. #26

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Posts
    507

    Re: Creating a Delay of 60 min

    Hi Guys

    Thats what I have before as a VB script that works with two functions:

    first bottun: REBOOT NOW
    second bottun: REboot in 60min

    and here is the script:
    VB Code:
    1. Const HWND_TOPMOST = -1
    2. Const HWND_NOTOPMOST = -2
    3. Const SWP_NOSIZE = &H1
    4. Const SWP_NOMOVE = &H2
    5. Const SWP_NOACTIVATE = &H10
    6. Const SWP_SHOWWINDOW = &H40
    7. Private Declare Sub SetWindowPos Lib "User32" (ByVal hWnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long)
    8.  
    9. Private Sub Form_Activate()
    10.        SetWindowPos Me.hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOACTIVATE Or SWP_SHOWWINDOW Or SWP_NOMOVE Or SWP_NOSIZE
    11. End Sub
    12.  
    13. Private Sub Command1_Click()
    14. Shell "shutdown.exe -r"
    15. End Sub
    16.  
    17.  
    18. Private Sub Command2_Click()
    19.   Shell "shutdown.exe -r -t 3600"
    20. End Sub

    Now if I want to add our delay functions to the existing existing script, is that how its supposed to look?

    VB Code:
    1. Option Explicit
    2.     Private Declare Function timeGetTime Lib "winmm.dll" () As Long
    3.     Public tmpTime As String
    4.     Public endTime As String
    5.     Public myApp As String
    6.     Public delayCount As Integer
    7.  
    8. Const HWND_TOPMOST = -1
    9. Const HWND_NOTOPMOST = -2
    10. Const SWP_NOSIZE = &H1
    11. Const SWP_NOMOVE = &H2
    12. Const SWP_NOACTIVATE = &H10
    13. Const SWP_SHOWWINDOW = &H40
    14. Private Declare Sub SetWindowPos Lib "User32" (ByVal hWnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long)
    15.  
    16. Private Sub Form_Activate()
    17.        SetWindowPos Me.hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOACTIVATE Or SWP_SHOWWINDOW Or SWP_NOMOVE Or SWP_NOSIZE
    18. End Sub
    19.  
    20. Private Sub Command1_Click()
    21. Shell "shutdown.exe -r"
    22. End Sub
    23.  
    24.  
    25. Private Sub Command2_Click()
    26.   Shell "shutdown.exe -r -t 3600"
    27. End Sub
    28.  
    29.  
    30.  
    31. Private Sub Command1_Click()
    32.     Dim startTime As String
    33.     Dim minutesToDelay As String
    34.         With Timer1
    35.             .Interval = 1
    36.             .Enabled = True
    37.         End With
    38.             myApp = "C:\windows\system32\shutdown.exe"
    39.             startTime = CStr(timeGetTime)
    40.             minutesToDelay = "60" 'must be a number character, otherwise Val returns 0
    41.             endTime = Val(startTime) + Val(minutesToDelay * 60000)
    42. End Sub
    43.  
    44. Private Sub Timer1_Timer()
    45.     tmpTime = Val(timeGetTime)
    46.         If tmpTime = endTime Then Shell "Notepad.exe", vbNormalFocus
    47.         If tmpTime = endTime And delayCount = 3 Then Shell myApp, vbNormalFocus
    48. End Sub

  27. #27

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Posts
    507

    Re: Creating a Delay of 60 min

    sorry guys, found a mistake, but that would be the final version.

    VB Code:
    1. Option Explicit
    2.     Private Declare Function timeGetTime Lib "winmm.dll" () As Long
    3.     Public tmpTime As String
    4.     Public endTime As String
    5.     Public myApp As String
    6.     Public delayCount As Integer
    7.  
    8. Const HWND_TOPMOST = -1
    9. Const HWND_NOTOPMOST = -2
    10. Const SWP_NOSIZE = &H1
    11. Const SWP_NOMOVE = &H2
    12. Const SWP_NOACTIVATE = &H10
    13. Const SWP_SHOWWINDOW = &H40
    14. Private Declare Sub SetWindowPos Lib "User32" (ByVal hWnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long)
    15.  
    16. Private Sub Form_Activate()
    17.        SetWindowPos Me.hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOACTIVATE Or SWP_SHOWWINDOW Or SWP_NOMOVE Or SWP_NOSIZE
    18. End Sub
    19.  
    20.  
    21. Private Sub Command2_Click()
    22.   Shell "shutdown.exe -r -t 3600"
    23. End Sub
    24.  
    25.  
    26.  
    27. Private Sub Command1_Click()
    28.     Dim startTime As String
    29.     Dim minutesToDelay As String
    30.         With Timer1
    31.             .Interval = 1
    32.             .Enabled = True
    33.         End With
    34.             myApp = "C:\windows\system32\shutdown.exe"
    35.             startTime = CStr(timeGetTime)
    36.             minutesToDelay = "60" 'must be a number character, otherwise Val returns 0
    37.             endTime = Val(startTime) + Val(minutesToDelay * 60000)
    38. End Sub
    39.  
    40. Private Sub Timer1_Timer()
    41.     tmpTime = Val(timeGetTime)
    42.         If tmpTime = endTime Then Shell "Notepad.exe", vbNormalFocus
    43.         If tmpTime = endTime And delayCount = 3 Then Shell myApp, vbNormalFocus
    44. End Sub

  28. #28
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Creating a Delay of 60 min

    What are you trying to do, wajdi ??? You asked about delays and you got two samples so I'm a little confused as to what else would you need?
    If your original question has been answered then set this thread as resolved and perhaps open new one if you have some other (unrelated to current) questions.

    Thanks.

  29. #29

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Posts
    507

    Re: Creating a Delay of 60 min

    Ok I will open a new thread, thanks for everyone and their input.

  30. #30

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width