Home > Software engineering >  [excuse me each teacher program delay in addition to sleep and timegettime better??)
[excuse me each teacher program delay in addition to sleep and timegettime better??)

Time:09-27


Sleep latency will suspend all the procedures, in VB6 this function might put other sub in running also stopped together ~

Timegettime not hang program and very precise, time delay, other code can also run,

But the downside is also very obvious, is high CPU ~


In VB6 I use the timer function, the timer function is not very precise calculation time but low CPU usage ~)

Inside the timer to delay 200 milliseconds and 5000 milliseconds, delay or other sub function during running, so can't use the sleep function

I use the timegettime function but CPU usage is high ~


Consult everybody the teacher, the timer to delay and can't sleep, is there any way to delay but relatively low CPU utilization?

CodePudding user response:

Don't wait delay within the function, but should keep running down, the end of the function directly,
And then start a timer, it is time to call this function again, function from the last state to continue execution down,

CodePudding user response:

The Timer interval down such as
Private sub timer1_timer ()
The static c as long
C=(c + 1) mod 10
The select case c
Case 1
.
Case 5
.
Case 8
.
End the select
End sub

CodePudding user response:

The method is very simple
The timegettime + sleep
The code

 
Public Function delay (T As Long)
Dim Savetime As Long
Savetime=timeGetTime '
note at the beginning of timeWhile timeGetTime & lt; Savetime + T 'circular wait
Sleep (10)
DoEvents' transfer of control
Wend
End the Function


Timegettime CPU high reason is that he kept in the loop of
Reason is that he doesn't sleep card response UI
It is good to combine the
Less than 200 best in here to sleep (), is greater than the number, or there will be a point card
Minimum of 1, but generally don't need to, the actual precision of less than 1 ms.

Can be posted

CodePudding user response:

reference 1st floor Tiger_Zhao response:
not silly inside function such as delay, but should keep running state, the end of the function directly,
And then start a timer, it is time to call this function again, function executed, down from the last state to continue

Hung. This method is good, a bit like interrupt limitations is only in their pure code in the process of implementation, if the middle calls to the function of the third party cannot.
Another is the VB timer is very weak chicken, not accurate enough, also easy to the operation of the high CPU delay.

CodePudding user response:

WallesCai
reference 4 floor response:
Quote: refer to 1st floor Tiger_Zhao response:

Don't wait delay within the function, but should keep running down, the end of the function directly,
And then start a timer, it is time to call this function again, function executed, down from the last state to continue

Hung. This method is good, a bit like interrupt limitations is only in their pure code in the process of implementation, if the middle calls to the function of the third party cannot.
Another is the VB timer is very weak chicken, not accurate enough, also easy to the operation of the high CPU delay.

I feel a bit like an asynchronous callback,
But sometimes bad running state save,
And a lot of code. To ensure running environment remains the same.
A line of sleep sometimes have to hundreds of lines to replace.

CodePudding user response:

Fyi:
 
The Begin VB. Form MainForm
The Begin VB. The Timer TimerTest
Enabled=1 'True
The Interval=1000
Left=0
Top=0
End
The Begin VB. The Timer TimerSleep
Enabled=0 'False
Left=0
Top=0
End
End
Dim done1 As Boolean
Sub sleep1 (ms)
Done1=False
TimerSleep. Interval=ms
TimerSleep. Enabled=True
Do
DoEvents
Loop Until done1
End Sub
Private Sub TimerSleep_Timer ()
TimerSleep. Interval=0
TimerSleep. Enabled=False
Done1=True
End Sub
Private Sub TimerTest_Timer ()
The Debug. Print "Test1"
Sleep1 400
The Debug. Print "Test2
"Sleep1 800
The Debug. Print "Test3
"End Sub

CodePudding user response:

reference 5 floor aspower_ reply:
Quote: refer to 4th floor WallesCai response:

Quote: refer to 1st floor Tiger_Zhao response:

Don't wait delay within the function, but should keep running down, the end of the function directly,
And then start a timer, it is time to call this function again, function executed, down from the last state to continue

Hung. This method is good, a bit like interrupt limitations is only in their pure code in the process of implementation, if the middle calls to the function of the third party cannot.
Another is the VB timer is very weak chicken, not accurate enough, also easy to the operation of the high CPU delay.

I feel a bit like an asynchronous callback,
But sometimes bad running state save,
And a lot of code. To ensure running environment remains the same.
A line of sleep sometimes have to hundreds of lines to replace.

Yeah, so there are a lot of limitations, VB does not support multithreading, after all. You have to like before DOS programming below to consider this problem (of course, not so exaggerated)
  •  Tags:  
  • API
  • Related