Home > Software engineering >  VB for beginners to the other a beginner's questions about the program contains a Sleep functio
VB for beginners to the other a beginner's questions about the program contains a Sleep functio

Time:10-01

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)


The Public Function Delay (DT As Long)

For I=0 To DT Step 2
DoEvents' operating rights to the system or procedure will temporarily no response
Sleep 1
Next I

End the Function

==============
The author who wrote the above program and such a comment in the main program,
Delay of 2000
End 'delay two seconds later the automatic End
================
My first question is Delay under the condition of Step 2 2000 isn't only Delay one second?
The odd thing is that when I run the author is right,
This is also related to the platform, I am using is a VB 6 mini,

The second problem is very strange flower: Delay two days before I use 60000 to Delay for one minute, generated the. Exe run normally, then today still use this exe into 10 minutes, 9 minutes later my own machine have modified the exe

CodePudding user response:

What circumstance to delay for 1 minute?

CodePudding user response:

I didn't see that: the author "to" where,
And I also clearly the actual test results do not support the building Lord,
According to my test, the result of the Delay of 2000 is more than 15 seconds,
If Delay of 60000, down more than 936 seconds, which is more than 15 minutes more than half (no actual operation, calculated according to the test result),
Is not the appearance of the building Lord said 9, 10 minutes,
Don't say with me "the machine" or "system", I run on the two hosts. Exe, no difference results:
A is WinXP, DELL host, 3.2 G dual-core (E5800);
Another is Windows 7 pro 32-bit, lenovo host, dual-core 3.0 G (G3220),

I run under the IDE, and compiled into exe running, the results (WinXP this one),
There is a post in BBS discussion before the "time accuracy", "accuracy" of Sleep was reached the level of less than 1 ms, only more than 10 ms,
Suggested search, the building Lord

My test code is as follows:
 Option Explicit 

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)


Private Function Delay (DT As Long)
Dim As Long I
For I=0 To DT Step 2
DoEvents' operating rights to the system or procedure will temporarily no response
Sleep 1
Next
End the Function


Private Sub Command1_Click ()
Dim t1!!! , t2!
Dim lDT As Long

Me. Cls
Print "2 seconds test:
"LDT=2000 'delay 2 seconds?
T1=the Timer () 'start time
Call Delay (lDT)
T2=the Timer () 'delay end time
Print a t1
Print t2
Print "actual delay:" & amp; The Format $(t2 - t1, "0.00")
Print "average period:" & amp; The Format $((t2 - t1)/(lDT/2 + 1), "0.0000")
Me. Print vbCrLf & amp; "A second test:
"LDT=1000 'delay of 1 second?
T1=the Timer () 'start time
Call Delay (lDT)
T2=the Timer () 'delay end time
Print a t1
Print t2
Print "actual delay:" & amp; The Format $(t2 - t1, "0.00")
Print "average period:" & amp; The Format $((t2 - t1)/(lDT/2 + 1), "0.0000")
Me. Print vbCrLf & amp; "0.1 seconds test:
"LDT=100 '0.1 seconds delay?
T1=the Timer () 'start time
Call Delay (lDT)
T2=the Timer () 'delay end time
Print a t1
Print t2
Print "actual delay:" & amp; The Format $(t2 - t1, "0.00")
Print "average period:" & amp; The Format $((t2 - t1)/(lDT/2 + 1), "0.0000")
End Sub

CodePudding user response:

Wanted to insert a picture of a running result, but not always successful,
(don't know what is problem, IE caused or company's network access restrictions)

Words said roughly:
Should be 2 seconds Delay 2000, theory, the test result is 15.64
Should be a 1 second Delay 1000, theory, test results is 7.83
Is about 0.1 seconds Delay 100, theory, test result was 0.80 seconds
Note is simple I use the Timer function to obtain the system time, the theory of "precision" of just 0.01 seconds, low precision,
So the result of the above "multiple proportion" have obvious differences, also is normal,

Code "average period" the calculation is to Delay every time () function For loop execution cycle,
Theory, main Sleep 1 at this time there should be 1 ms (or Step 2 according to the author, should be 2 ms),
But my test results, basically be 15.63 ms,
The gap is very large!!!!!!

CodePudding user response:

In other words, if you want to "accurate" to delay, would by Sleep cycle, was "unreliable"!!!!!!

If want to make more accurate delay, must be entered "delay" gets the current time (starting point), then the Do loop/Sleep/DoEvents
In the Do loop to get "current time", calculation time interval to the starting point, when greater than or equal to delay, jump on the Do loop, return end of the time delay,
So you can more accurate time delay, delay "precision" depends on the precision of the "time",
In what way, just look at the actual demand,

CodePudding user response:

Thank you, sleep "accuracy" I know that far-fetched, but still don't know much about the Delay function,

The Public Function Delay (DT As Long)

For I=0 To DT Step 2
DoEvents' operating rights to the system or procedure will temporarily no response
Sleep 1
Next I

End the Function

Assuming that sleep is a standard of 1 ms, in step 2, under the condition of Delay 2000, theoretically is 1 second,

CodePudding user response:

reference 5 floor Malone_karl reply:
thank you very much, the "precision" sleep I know unreliable, but still don't know much about the Delay function,

The Public Function Delay (DT As Long)

For I=0 To DT Step 2
DoEvents' operating rights to the system or procedure will temporarily no response
Sleep 1
Next I

End the Function

Assuming that sleep is a standard of 1 ms, in step 2, under the condition of Delay 2000, theoretically is 1 second,


Obviously, DoEvents cycle call in your code this way, he needs time is uncertain, but you also said that "Delay 2000, theoretically is 1 second, absurd:)

CodePudding user response:

Private Function Delay (DT As Long)
Dim As Long I
For I=0 To DT Step 2
DoEvents' operating rights to the system or procedure will temporarily no response
Sleep 1
Next
End the Function

This kind of method of time delay is very inaccurate. By machine hardware, running environment, running thread influence factors, such as
 Private Function Delay (DT As Long) 
Dim t0 As Long

T0=GetTickcount ()
The do while 1
If the gettickcount - t0 & gt; Dt then exit the do
DoEvents' operating rights to the system or procedure will temporarily no response
Sleep 1
Loop
End the Function

CodePudding user response:

15 ms Windows delay precision of material
  • Related