Results 1 to 7 of 7

Thread: Save / Set timer

  1. #1

    Thread Starter
    Frenzied Member CyberCarsten's Avatar
    Join Date
    Sep 1999
    Location
    Aalborg Ø, Denmark
    Posts
    1,544

    Post

    Hi!
    I'm building a program that can
    shutdown the computer after a
    certain time.
    Then i want the user to
    adjust the timer interval, but i can't
    get it to work!!
    The must retrieve the timer-interval
    from the registry or from a file...etc...

    / CyberCarsten [^Cc^]


  2. #2
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    Post

    Try using the Timer Function in conjunction with a Timer Control, eg.
    Code:
    Private tInterval As Long 'Interval in Minutes
    Private Sub Timer1_Timer()
        Static tTimer As Single
        If tTimer = 0 Then tTimer = Timer
        If (Timer - tTimer) >= (tInterval * 60) Then
            'ShutDown the Computer
            Timer1.Enabled = False
        End If
    End Sub
    Then, to Adjust, Save/Restore the Interval, simply Modify the value of tInterval in Minutes.


    ------------------
    Aaron Young
    Analyst Programmer
    [email protected]
    [email protected]

  3. #3
    Hyperactive Member razzaj's Avatar
    Join Date
    Oct 1999
    Location
    jounieh
    Posts
    261

    Post

    well .. what young proposed is fine ...
    but this way when the computer starts again
    the time the user set will be erased because it is in memory.
    using registry is fine but here is a simpler way :

    start a blanc project
    - insert a timer control
    - insert a text control
    - insert a command button
    in module :


    Declare Function ExitWindowsEx Lib "user32" _
    (ByVal uFlags As Long, ByVal dwReserved As Long) As Long


    public Const EWX_SHUTDOWN = &H1
    public timetoclose as long


    in form code :

    - in text_keypress
    if keyascii = 13 and text1 <> "" then
    open "c:\shutdown.set" for output as #1
    print #1 , text1
    close #1
    timetoclose = val (text1) * 1000
    me.hide
    initcounter
    end if
    - in timer1_timer
    timer1.enabled = false
    ExitWindowsEx EWX_SHUTDOWN
    end sub

    - in form_load
    dim a as string
    open "c:\shutdown.set" for input as #1
    input #1 , a
    close #1
    text1 = val(a)
    timetoclose = text1* 1000
    initcounter
    end sub

    - in command1_click
    me.hide
    initcounter
    end sub


    - create a sub called initcounter
    public function initcounter ()
    timer1.interval = timetoclose
    timer1.enabled = true
    end function


    user's manual :
    -------------------
    1 - i think that is it i havent tried it
    but if u have any problem just message me
    back.
    2 - ur aim was not too clear so i tryed to
    give the most satisfying answer
    3- before starting the program just go to
    "c:\" and create a file called
    "shutdown.set" write in it anything
    a number for instance u need to do this
    only the first time u run the program
    . icould have given u a more complete
    code but it is late now i have to sleep .
    4- when the form shows input in the textbox
    a number (minutes) and PRESS ENTER
    5- if the next time u start the program
    u dont want to change the time just press
    the command button
    6 - i know this is far form being the best
    and efficient code but i really feel
    sleepy.
    7 - if u need any help again .... just
    message me anytime i ll be gald to help


    - Regards -
    - razzaj -

  4. #4

    Thread Starter
    Frenzied Member CyberCarsten's Avatar
    Join Date
    Sep 1999
    Location
    Aalborg Ø, Denmark
    Posts
    1,544

    Post

    Thanx guys!!!

  5. #5

    Thread Starter
    Frenzied Member CyberCarsten's Avatar
    Join Date
    Sep 1999
    Location
    Aalborg Ø, Denmark
    Posts
    1,544

    Post


    razzaj , that didn't work...


  6. #6
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    Post

    The Code I Posted will work, all you have to do is save/set the value of tInterval, eg.
    Code:
    Private tInterval As Long 'Interval in Minutes
    
    Private Sub Timer1_Timer()
        Static tTimer As Single
        Dim lSecs As Long
        Dim iMins As Integer
        Dim iHrs As Integer
        
        If tTimer = 0 Then tTimer = Timer
        If (Timer - tTimer) >= (tInterval * 60) Then
            'ShutDown the Computer
            Timer1.Enabled = False
        End If
        lSecs = (tInterval * 60) - Int(Timer - tTimer)
        iHrs = Int((lSecs / 60) / 60)
        iMins = Int((lSecs / 60) - (iHrs * 60))
        lSecs = (lSecs - (iMins * 60) - (iHrs * 360)) Mod 60
        Caption = "Shutdown in " & _
        Right("00" & iHrs, 2) & ":" & Right("00" & iMins, 2) & ":" & Right("00" & lSecs, 2)
    End Sub
    
    Private Sub Form_Load()
        'Get the Stored Time Interval, Default to 1 Hour (60 Mins).
        tInterval = Val(GetSetting(App.Title, "Interval", "Value", "60"))
        If MsgBox("Do You Wish to Change the Time Interval?" & _
        vbCrLf & vbCrLf & _
        "It is Currently..  " & tInterval & _
        " Minutes.", vbQuestion + vbYesNo, "Timer Interval") = vbYes Then
            tInterval = Val(InputBox("Enter a new Interval in Minutes: ", "Set Interval", _
            tInterval))
        End If
        'Save the Interval to the Registry
        SaveSetting App.Title, "Interval", "Value", Trim(Str(tInterval))
        Timer1.Interval = 100
    End Sub
    ------------------
    Aaron Young
    Analyst Programmer
    [email protected]
    [email protected]

  7. #7

    Thread Starter
    Frenzied Member CyberCarsten's Avatar
    Join Date
    Sep 1999
    Location
    Aalborg Ø, Denmark
    Posts
    1,544

    Post

    Thank you Aaron!!!
    That worked!! : - )

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