473,397 Members | 2,028 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Do nothing but wait loop..

Hello all,
The sleep() method hangs up the application and does not respond to
events. So, I wrote a small delay loop that will allow the application
to respond to events.

'Use Delay(500) to delay the application for 500ms.

Public blnSleepTimeExpired As Boolean

Public Sub Delay(ByVal intSleepTimems As Integer)
'set interval.
Dim intCount As Integer
tmrDelay = New Timer
tmrDelay.Interval = intSleepTimems
blnSleepTimeExpired = False

tmrDelay.Start()
Do While (Not blnSleepTimeExpired)
intCount += 1
If (intCount Mod 25) = 0 Then DoEvents()
'Debug.WriteLine(intSleepTimems & " Count: " & intCount)
Loop
tmrDelay.Stop()
End Sub

Private Sub tmrDelay_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles tmrDelay.Tick
blnSleepTimeExpired = True
End Sub

The code works most of the time but hangs up the application once a
while.

Can anyone help me with code that would do nothing but wait for certain
time.

I appreciate any help on this issue.
Thanks.

May 4 '06 #1
10 8275
Tim
a bit of vb6 code, but it may work

ti=timer
do
doevents
loop until timer>ti+1

May 4 '06 #2
The code hangs up because you are waiting intensely. The thread runs at
full speed checking all the time if something is happening. That will
impact all other processes in the computer.

Put a sleep in the loop, and it will calm down:

Do While (Not blnSleepTimeExpired)
Sleep(10)
DoEvents()
Loop
IdleBrain wrote:
Hello all,
The sleep() method hangs up the application and does not respond to
events. So, I wrote a small delay loop that will allow the application
to respond to events.

'Use Delay(500) to delay the application for 500ms.

Public blnSleepTimeExpired As Boolean

Public Sub Delay(ByVal intSleepTimems As Integer)
'set interval.
Dim intCount As Integer
tmrDelay = New Timer
tmrDelay.Interval = intSleepTimems
blnSleepTimeExpired = False

tmrDelay.Start()
Do While (Not blnSleepTimeExpired)
intCount += 1
If (intCount Mod 25) = 0 Then DoEvents()
'Debug.WriteLine(intSleepTimems & " Count: " & intCount)
Loop
tmrDelay.Stop()
End Sub

Private Sub tmrDelay_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles tmrDelay.Tick
blnSleepTimeExpired = True
End Sub

The code works most of the time but hangs up the application once a
while.

Can anyone help me with code that would do nothing but wait for certain
time.

I appreciate any help on this issue.
Thanks.

May 4 '06 #3
Hello, IdleBrain:

You should not run a tight loop with DoEvents. You should consider changing your code either
·Running your job in a separate thread. You can safely sleep it whenever you want.
·Splitting your job. See this sample:
Sub Job()
'Do something.
'Wait some time.
'Do the rest of the job.
End Sub

Converts to:

Sub Job()
'Do something.
Me.TimerJob.Interval = Sometime
Me.TimerJob.Start
End Sub

Sub TimerJob_Tick(...) handles TimerJob.Tick
Me.TimerJob.Stop
'Do the rest of the job.
End sub

Well, this last solution is rarely suitable, but you may find a variation or use BackgroundWorker or threading the way you like.

Regards.
"IdleBrain" <in**************@yahoo.com> escribió en el mensaje news:11**********************@j33g2000cwa.googlegr oups.com...
| Hello all,
| The sleep() method hangs up the application and does not respond to
| events. So, I wrote a small delay loop that will allow the application
| to respond to events.
|
| 'Use Delay(500) to delay the application for 500ms.
|
| Public blnSleepTimeExpired As Boolean
|
| Public Sub Delay(ByVal intSleepTimems As Integer)
| 'set interval.
| Dim intCount As Integer
| tmrDelay = New Timer
| tmrDelay.Interval = intSleepTimems
| blnSleepTimeExpired = False
|
| tmrDelay.Start()
| Do While (Not blnSleepTimeExpired)
| intCount += 1
| If (intCount Mod 25) = 0 Then DoEvents()
| 'Debug.WriteLine(intSleepTimems & " Count: " & intCount)
| Loop
| tmrDelay.Stop()
| End Sub
|
| Private Sub tmrDelay_Tick(ByVal sender As System.Object, ByVal e As
| System.EventArgs) Handles tmrDelay.Tick
| blnSleepTimeExpired = True
| End Sub
|
| The code works most of the time but hangs up the application once a
| while.
|
| Can anyone help me with code that would do nothing but wait for certain
| time.
|
| I appreciate any help on this issue.
| Thanks.

May 4 '06 #4
I have implemented Tim's and Goran's ideas... They seem to be working
okay.
I cannot implement Jose's ideas because...I use the Delay() method
through out the application in the place of Sleep().

Any more thoughts to respond to the system events in a better way?

May 4 '06 #5
Take a look at delegates and event callbacks. For examples, look at the
system.threading.timer class.

Mike Ober.

"IdleBrain" <in**************@yahoo.com> wrote in message
news:11**********************@j33g2000cwa.googlegr oups.com...
I have implemented Tim's and Goran's ideas... They seem to be working
okay.
I cannot implement Jose's ideas because...I use the Delay() method
through out the application in the place of Sleep().

Any more thoughts to respond to the system events in a better way?


May 5 '06 #6
Jose,
You should not run a tight loop with DoEvents. You should consider changing
your code either
Running your job in a separate thread. You can safely sleep it whenever you
want.
Splitting your job. See this sample:


I have read this more, can you explain to us why.

Multi.Threading takes a lot of resources and performances so there should be
a very good reason why you write this above. I have the idea that a lot of
people are just paroting, therefor why?

Cor
May 5 '06 #7
Hello Cor,

It's known that .NET 1.1 with visual styles enabled has performance problems in such situation. And DoEvents requieres you to handle things like event reentrance and forms closing in the middle of a procedure.
Multithreading takes some resources, but the performance may suffer only when you create the thread, not when you use it.
Anyway performance seems not to be a problem for IdleBrain, so he can use whatever solution he wants.

Regards.
"Cor Ligthert [MVP]" <no************@planet.nl> escribió en el mensaje news:e3**************@TK2MSFTNGP05.phx.gbl...
| Jose,
|
| >You should not run a tight loop with DoEvents. You should consider changing
| >your code either
| >Running your job in a separate thread. You can safely sleep it whenever you
| >want.
| >Splitting your job. See this sample:
|
| I have read this more, can you explain to us why.
|
| Multi.Threading takes a lot of resources and performances so there should be
| a very good reason why you write this above. I have the idea that a lot of
| people are just paroting, therefor why?
|
| Cor

May 5 '06 #8
Hello all,
I started using GetTickCount()..Seems to work better than the timer in
my application.
Thanks for all your comments.

Private Declare Function GetTickCount Lib "Kernel32" () As Integer

Public Sub Delay(ByVal intSleepTimems As Integer)
Dim lngTime As Long
lngTime = GetTickCount()

Do While (intSleepTimems > GetTickCount() - lngTime)
DoEvents()
Loop
End Sub

May 5 '06 #9
I'd suggest that you throw in a short Sleep in the loop also.

IdleBrain wrote:
Hello all,
I started using GetTickCount()..Seems to work better than the timer in
my application.
Thanks for all your comments.

Private Declare Function GetTickCount Lib "Kernel32" () As Integer

Public Sub Delay(ByVal intSleepTimems As Integer)
Dim lngTime As Long
lngTime = GetTickCount()

Do While (intSleepTimems > GetTickCount() - lngTime)
DoEvents()
Loop
End Sub

May 5 '06 #10
Just for completion, GetTickCount returns an UInteger. Also, your Delay sub may not work as expected if it is running when the count wraps to Integer.MinValue. This happens after ~25 days of system activity and then every ~50 days, so it's rare to affect your program, but if you use it extensively, you must know it.

Regards.
"IdleBrain" <in**************@yahoo.com> escribió en el mensaje news:11*********************@j33g2000cwa.googlegro ups.com...
| Hello all,
| I started using GetTickCount()..Seems to work better than the timer in
| my application.
| Thanks for all your comments.
|
| Private Declare Function GetTickCount Lib "Kernel32" () As Integer
|
| Public Sub Delay(ByVal intSleepTimems As Integer)
| Dim lngTime As Long
| lngTime = GetTickCount()
|
| Do While (intSleepTimems > GetTickCount() - lngTime)
| DoEvents()
| Loop
| End Sub

May 5 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Rasmus Grøndahl Olsen | last post by:
I have tried to write a wait function but it seems like it will not brake the while loop. I tried two different solutions. Can anyone tell me what I am doing wrong, and come with another...
9
by: cantelow | last post by:
Hi. I've got a program printing multiple pdf reports, and I need to wait for completion of various associated operations- The pdf printer prints to one file, then I need to copy that where I need...
1
by: swin | last post by:
I've a wrapper class around axBrowser which logs into a site, navigates to a data page and harvests info from it. It works fine for a single data page (passing it a key for the data), but my...
6
by: yaron | last post by:
Hi, I wanted to use Monitor.Wait but the millisecondsTimeout parameter is of type int, and also with the TimeSpan param it can't be greater than MaxInt32 milliseconds. What to do ? ...
15
by: Snuyt | last post by:
Hello, I want the program to wait a few seconds between executing code. It should look something like this: public sub xx() ...code... wait(2) 'wait 2 seconds ...code...
4
by: Terry Olsen | last post by:
I have loop that calls a Sub that runs the following code: Dim WinZip As System.Diagnostics.Process Dim args As String = " -Pru -ex " & lblFolder.Text & "\" & PCName & ".zip @""" & appPth &...
11
by: ryan | last post by:
Hi, I've omitted a large chunk of the code for clarity but the loop below is how I'm calling a delegate function asynchronously. After I start the each call I'm incrementing a counter and then...
25
by: abbylee26 | last post by:
my page works fine if the db search finds at least one record that satifies the query. but if it does not find a match I get the following error message. Error Type: ADODB.Field (0x80020009)...
9
by: Nightfarer | last post by:
Hello. I have this loop started by a button Click event: For i = 0 To AlphabetArray.GetUpperBound(0) AlphabetArray.SetValue(letter, i) ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.