Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > General > Program freezes while using "Sleep"


Reply
 
Thread Tools Display Modes
  #1  
Old 12-28-2003, 04:41 PM
phyko phyko is offline
Freshman
 
Join Date: Dec 2003
Posts: 23
Default Program freezes while using "Sleep"


intSeconds = (hours * 3600) + (minutes * 60) + seconds
timeLeft = intSeconds

For seconds = 0 to intSeconds
timeLeft = timeLeft - 1
Sleep 1000
Next seconds



When I run my program and hit the button that is supposed to count down the seconds, it just freezes and I have to end task VB. Any help appreciated, or another way to make a counter. Thank you.
Reply With Quote
  #2  
Old 12-28-2003, 05:18 PM
reboot's Avatar
reboot reboot is offline
Keeper of foo

Retired Moderator
* Guru *
 
Join Date: Nov 2001
Location: Graceland
Posts: 15,612
Default

Of course it does. Why do you have Sleep in the loop in the first place?
Reply With Quote
  #3  
Old 12-28-2003, 05:19 PM
phyko phyko is offline
Freshman
 
Join Date: Dec 2003
Posts: 23
Default

Quote:
Originally Posted by reboot
Of course it does. Why do you have Sleep in the loop in the first place?



yea i just realized it would, i have it because i wanted to make a countdown.. pausing every sec and so on...
Reply With Quote
  #4  
Old 12-28-2003, 05:52 PM
Squirm's Avatar
Squirm Squirm is offline
Political Coder

Retired Moderator
* Guru *
 
Join Date: Mar 2001
Location: London, England
Posts: 8,037
Default

Perhaps you should consider a Timer control instead?
__________________
Search the forums | Use [vb][/vb] tags | Still IRCing
Reply With Quote
  #5  
Old 12-28-2003, 05:59 PM
edb edb is offline
Centurion
 
Join Date: Jul 2003
Posts: 128
Default Use this...

Put this code into a module:
---------------------------------------------------------------------
Option Explicit

Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type

Private Const WAIT_ABANDONED& = &H80&
Private Const WAIT_ABANDONED_0& = &H80&
Private Const WAIT_FAILED& = -1&
Private Const WAIT_IO_COMPLETION& = &HC0&
Private Const WAIT_OBJECT_0& = 0
Private Const WAIT_OBJECT_1& = 1
Private Const WAIT_TIMEOUT& = &H102&

Private Const INFINITE = &HFFFF
Private Const ERROR_ALREADY_EXISTS = 183&

Private Const QS_HOTKEY& = &H80
Private Const QS_KEY& = &H1
Private Const QS_MOUSEBUTTON& = &H4
Private Const QS_MOUSEMOVE& = &H2
Private Const QS_PAINT& = &H20
Private Const QS_POSTMESSAGE& = &H8
Private Const QS_SENDMESSAGE& = &H40
Private Const QS_TIMER& = &H10
Private Const QS_MOUSE& = (QS_MOUSEMOVE _
Or QS_MOUSEBUTTON)
Private Const QS_INPUT& = (QS_MOUSE _
Or QS_KEY)
Private Const QS_ALLEVENTS& = (QS_INPUT _
Or QS_POSTMESSAGE _
Or QS_TIMER _
Or QS_PAINT _
Or QS_HOTKEY)
Private Const QS_ALLINPUT& = (QS_SENDMESSAGE _
Or QS_PAINT _
Or QS_TIMER _
Or QS_POSTMESSAGE _
Or QS_MOUSEBUTTON _
Or QS_MOUSEMOVE _
Or QS_HOTKEY _
Or QS_KEY)

Private Declare Function CreateWaitableTimer Lib "kernel32" _
Alias "CreateWaitableTimerA" ( _
ByVal lpSemaphoreAttributes As Long, _
ByVal bManualReset As Long, _
ByVal lpName As String) As Long

Private Declare Function OpenWaitableTimer Lib "kernel32" _
Alias "OpenWaitableTimerA" ( _
ByVal dwDesiredAccess As Long, _
ByVal bInheritHandle As Long, _
ByVal lpName As String) As Long

Private Declare Function SetWaitableTimer Lib "kernel32" ( _
ByVal hTimer As Long, _
lpDueTime As FILETIME, _
ByVal lPeriod As Long, _
ByVal pfnCompletionRoutine As Long, _
ByVal lpArgToCompletionRoutine As Long, _
ByVal fResume As Long) As Long

Private Declare Function CancelWaitableTimer Lib "kernel32" ( _
ByVal hTimer As Long)

Private Declare Function CloseHandle Lib "kernel32" ( _
ByVal hObject As Long) As Long

Private Declare Function WaitForSingleObject Lib "kernel32" ( _
ByVal hHandle As Long, _
ByVal dwMilliseconds As Long) As Long

Private Declare Function MsgWaitForMultipleObjects Lib "user32" ( _
ByVal nCount As Long, _
pHandles As Long, _
ByVal fWaitAll As Long, _
ByVal dwMilliseconds As Long, _
ByVal dwWakeMask As Long) As Long

Public Sub Wait(lNumberOfSeconds As Double)
Dim ft As FILETIME
Dim lBusy As Long
Dim lRet As Long
Dim dblDelay As Double
Dim dblDelayLow As Double
Dim dblUnits As Double
Dim hTimer As Long

hTimer = CreateWaitableTimer(0, True, App.EXEName & "Timer")

If Err.LastDllError = ERROR_ALREADY_EXISTS Then
' If the timer already exists, it does not hurt to open it
' as long as the person who is trying to open it has the
' proper access rights.
Else
ft.dwLowDateTime = -1
ft.dwHighDateTime = -1
lRet = SetWaitableTimer(hTimer, ft, 0, 0, 0, 0)
End If

' Convert the Units to nanoseconds.
dblUnits = CDbl(&H10000) * CDbl(&H10000)
dblDelay = CDbl(lNumberOfSeconds) * 1000 * 10000

' By setting the high/low time to a negative number, it tells
' the Wait (in SetWaitableTimer) to use an offset time as
' opposed to a hardcoded time. If it were positive, it would
' try to convert the value to GMT.
ft.dwHighDateTime = -CLng(dblDelay / dblUnits) - 1
dblDelayLow = -dblUnits * (dblDelay / dblUnits - _
Fix(dblDelay / dblUnits))

If dblDelayLow < CDbl(&H80000000) Then
' &H80000000 is MAX_LONG, so you are just making sure
' that you don't overflow when you try to stick it into
' the FILETIME structure.
dblDelayLow = dblUnits + dblDelayLow
End If

ft.dwLowDateTime = CLng(dblDelayLow)
lRet = SetWaitableTimer(hTimer, ft, 0, 0, 0, False)

Do
' QS_ALLINPUT means that MsgWaitForMultipleObjects will
' return every time the thread in which it is running gets
' a message. If you wanted to handle messages in here you could,
' but by calling Doevents you are letting DefWindowProc
' do its normal windows message handling---Like DDE, etc.
lBusy = MsgWaitForMultipleObjects(1, hTimer, False, _
INFINITE, QS_ALLINPUT&)
DoEvents
Loop Until lBusy = WAIT_OBJECT_0

' Close the handles when you are done with them.
CloseHandle hTimer

End Sub


Now... use this in your form:
---------------------------------------------------------------------
Wait (0.2) 'Wait is in seconds... so this will wait for 1/5 sec.
Reply With Quote
  #6  
Old 12-28-2003, 06:39 PM
phyko phyko is offline
Freshman
 
Join Date: Dec 2003
Posts: 23
Default

That worked, although long code

but I still can't get the count down to work.

intSeconds = 100
tLeft = 1

While (tLeft > 0)
Wait (1)
tLeft = intSeconds - 1
Wend


It only subracts once and then it stops.
Reply With Quote
  #7  
Old 12-28-2003, 06:49 PM
LayZBoy00 LayZBoy00 is offline
Banned
 
Join Date: Feb 2004
Posts: 149
Default

Code:
Public Time As Currency Private Sub Form_Load() Time = 60 End Sub Private Sub Timer1_Timer() Time = Time - 1 MsgBox Time End Sub

timer1.interval = 1000 btw
Reply With Quote
  #8  
Old 12-28-2003, 07:00 PM
phyko phyko is offline
Freshman
 
Join Date: Dec 2003
Posts: 23
Default

Thank you everyone who helped. I got it to work by doing this:

If intSeconds = 0 Then
tLeft = "Completed!"
Else
Call cDown
End If
End Sub



Private Sub cDown()
While (tLeft <> 0)
Wait (1)
tLeft.Caption = tLeft - 1
Wend
End Sub
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Selecting an item in a list box in an external program JohnVM General 0 10-10-2003 04:38 PM
Selecting an item in a list box in an external program JohnVM API 0 10-10-2003 04:36 PM
handling large text files - program freezes!! badCorky File I/O and Registry 3 09-20-2003 03:56 PM
VB Freezes after I stop a Winsock program? M.C Communications 2 07-13-2003 04:45 AM
Resizing array freezes my program Kamochan Game Programming 5 02-28-2003 05:17 PM

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->