473,804 Members | 3,697 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 blnSleepTimeExp ired As Boolean

Public Sub Delay(ByVal intSleepTimems As Integer)
'set interval.
Dim intCount As Integer
tmrDelay = New Timer
tmrDelay.Interv al = intSleepTimems
blnSleepTimeExp ired = False

tmrDelay.Start( )
Do While (Not blnSleepTimeExp ired)
intCount += 1
If (intCount Mod 25) = 0 Then DoEvents()
'Debug.WriteLin e(intSleepTimem s & " Count: " & intCount)
Loop
tmrDelay.Stop()
End Sub

Private Sub tmrDelay_Tick(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles tmrDelay.Tick
blnSleepTimeExp ired = 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 8310
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 blnSleepTimeExp ired)
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 blnSleepTimeExp ired As Boolean

Public Sub Delay(ByVal intSleepTimems As Integer)
'set interval.
Dim intCount As Integer
tmrDelay = New Timer
tmrDelay.Interv al = intSleepTimems
blnSleepTimeExp ired = False

tmrDelay.Start( )
Do While (Not blnSleepTimeExp ired)
intCount += 1
If (intCount Mod 25) = 0 Then DoEvents()
'Debug.WriteLin e(intSleepTimem s & " Count: " & intCount)
Loop
tmrDelay.Stop()
End Sub

Private Sub tmrDelay_Tick(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles tmrDelay.Tick
blnSleepTimeExp ired = 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.Int erval = Sometime
Me.TimerJob.Sta rt
End Sub

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

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

Regards.
"IdleBrain" <in************ **@yahoo.com> escribió en el mensaje news:11******** **************@ j33g2000cwa.goo glegroups.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 blnSleepTimeExp ired As Boolean
|
| Public Sub Delay(ByVal intSleepTimems As Integer)
| 'set interval.
| Dim intCount As Integer
| tmrDelay = New Timer
| tmrDelay.Interv al = intSleepTimems
| blnSleepTimeExp ired = False
|
| tmrDelay.Start( )
| Do While (Not blnSleepTimeExp ired)
| intCount += 1
| If (intCount Mod 25) = 0 Then DoEvents()
| 'Debug.WriteLin e(intSleepTimem s & " Count: " & intCount)
| Loop
| tmrDelay.Stop()
| End Sub
|
| Private Sub tmrDelay_Tick(B yVal sender As System.Object, ByVal e As
| System.EventArg s) Handles tmrDelay.Tick
| blnSleepTimeExp ired = 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.threadin g.timer class.

Mike Ober.

"IdleBrain" <in************ **@yahoo.com> wrote in message
news:11******** **************@ j33g2000cwa.goo glegroups.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******** ******@TK2MSFTN GP05.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

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

Similar topics

2
11437
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 suggestion? If I call the function like this: wait(500); it should wait 500ms right? function wait(time) { while(1){ setTimeout("break;",time); } }
9
5893
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 it, then delete the old file, repeat with the next report. I need to wait for each step to complete or I get errors due to the previous thing being open and not done yet. Is there any elegant way to do that? My FileCopy in particular has...
1
1862
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 next task is to put it in a loop so that I can retrieve multiple pages of data. My problem is that how can I control the loop such that it won't try and get the next page of data until the previous one has completed - bearing in mind that I have...
6
1846
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 ? Thanks.
15
22182
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
2814
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 & "WksBkup.txt""" WinZip.Start("c:\Program Files\WinZip\wzzip.exe", args) WinZip.WaitForExit() Do If WinZip.HasExited = True Then Exit Do Loop
11
6974
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 making the main thread sleep until the counter gets back to zero. The call back function for each call decrements the counter. Is there a better way to make the thread wait until all calls are complete besides using the counter? I've seen some things...
25
2471
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) Either BOF or EOF is True, or the current record has been deleted. Requested operation requires a current record. What do I need to change here to make this just display the page with
9
5649
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) Alphabet.Lettertxtbox.Clear() Next i
0
9706
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10577
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10332
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10320
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9150
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7620
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5521
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3820
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2991
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.