hi,

i am working on a program that locks your computer after a preset time, sort of like the type you would find in an internet cafe etc, but it doesnt work properly.


when its unlocked and a time enterd it passes the time (in minutes to a form that shows your time, the time is placed in a variable (minutevar), another variable (secondsvar) is set to 60 and a timer is run.

The timer counts down in 1000 ms increments and each time its sub is triggerd it subtracts 1 from the second variable (secondsvar) and then updates the display. it then subtracts 1 from the first variable (minutevar) IF secondsvar = 0 then it updates the display.

IF minutevar = 0 then it shows the Lock form and hides its self otherwise it continues.

the first time the timer is run, it works perfectly, the time is set, the timer counts down and it locks the computer when its supposed to, then when i try and unlock it again it unlocks the time is enterd and the timer set BUT the timer has frozen and no longer counts down, i have tried setting a breakpoint on the timer, it works the first time and breaks but the second time it doesnt break, which indicates to me that the timer isnt running.


If Anyone has any ideas on how to fix this problem please let me know, i can provide source code if needed in either text or file form.

Dani AI

Generated

A concise troubleshooting note for and anyone following this thread: the reported symptom — the Tick handler fires during the first session but does not fire after unlocking and restarting — most commonly means the timer control or its event hookup is no longer active after the lock/unlock cycle, or the UI thread is being blocked so Tick events never get delivered. That behavior fits several distinct failure modes; the checklist below shows the safest checks and a more robust countdown pattern to avoid fragile counter logic.

  • Confirm the same Timer instance is still alive after unlocking. If the form that held the Timer was Closed (disposed) instead of Hidden, restarting code might be acting on a disposed control or a new form instance without the Tick handler attached.
  • Verify the Timer is actually started after unlocking (set a breakpoint on the code that calls Start/Enabled = True or write a tiny log entry). If event wiring was lost, the Tick sub will never run even though the control exists.
  • Look for UI-thread blocking calls between lock and restart (long loops, Thread.Sleep, synchronous IO). Timers that rely on the UI message pump will not fire while the thread is busy.
  • If a background/system timer is used instead of a UI timer, ensure any UI updates are marshaled with Invoke/BeginInvoke to avoid cross-thread exceptions that silently stop updates.

A more robust approach is to record the target end time and compute remaining time on each tick instead of decrementing minute/second counters. Example (VB.NET):

Dim endTime As DateTime

Sub StartCountdown(minutes As Integer)
    endTime = DateTime.Now.AddMinutes(minutes)
    Timer1.Interval = 1000
    Timer1.Start()
End Sub

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    Dim remaining As TimeSpan = endTime - DateTime.Now
    If remaining.TotalSeconds <= 0 Then
        Timer1.Stop()
        ' perform lock sequence here (show lock form, hide this form, etc.)
    Else
        lblTime.Text = String.Format("{0}:{1:D2}", Math.Floor(remaining.TotalMinutes), remaining.Seconds)
    End If
End Sub

This avoids drift, simplifies restart logic, and makes it easier to see whether the timer stopped because of disposal, event wiring, or thread blocking. As suggested, counting idle time can work, but the DateTime method is more resilient; a minimal reproducible snippet showing the Start/Stop and lock/unlock code will quickly reveal which failure mode applies.

Recommended Answers

All 4 Replies

PS. I have tried many diffrent things and nothing seems to work for me

i am also working with this type of coding to lock like in the cyber cafe,if u plz,give me ur full code then i can check this with my code and hope that we both success in this code can u mail me your code file on my id---
if i done this then i forward the correct code to you ok

I'm not sure I even still have the code, this was 3 years ago.

Hi,

Say, you want to lock the Computer, if there is no activity for 5 mins continus.. then you need to have a Timer.
Timer's Interval is 1000 ms(1 sec)
By Default Timer is enable all the time.
Declare a FormLevel Variable
MyVar as Integer.

Make Form's property Keypreview=True.
In KeyUp , KeyPress, MouseMove , MouseUp events of form, make
MyVar = 0
Timer1.Enabled = True

In Timer's Timer event Increment
MyVar = MyVar+1
If MyVar >= 300 Then
' Write Code To Lock the Computer here
Timer1.Enabled =False
MyVar = 0
End If

Regards
Veena

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.