Doevents application

zhaozj2021-02-11  214

Transfer control to allow the operating system to handle other events.

The DoEvents function returns an Integer to represent the number of forms opened in the Visual Basic's stand-alone version, for example, Visual Basic, Professional, in other applications, doevents returns 0.

DoEvents will pass control to the operating system. When the operating system processes the events in the complex column, and after all the keys in the SendKeys queue are sent, the control is returned.

DoEvents For simplified processes such as allowing users to cancel a start-up process - such as searching for a file - especially useful. For long-term processes, abandon control is best to use timers or through delegated tasks to the ActiveX EXE part. In the future, the task is completely independent of the application, multitasking and time sheet by the operating system.

CAUTION Make sure that the process of controlling the control is abandoned in DOEvents, before the first doevents returns, you cannot be called again by the other part of the code; otherwise it will produce unpredictable results. In addition, if other applications may interact with this procedure, do not use DOEvents because the control is not discarded at this time.

Use doevents

Although the Timer event is the best tool for background processing, the situation is more likely to time consuming, but the doevents function provides a simple way to cancel the task. For example, the following code will display a "process" button, when you click this button, it will become a "CANCEL" button. Click the button again and will interrupt the task being executed.

'This button title is all instances of the "Process" Private Sub Command1_Click ()' process shared static variables. Static blnProcessing As Boolean Dim lngCt As Long Dim intYieldCt As Integer Dim dblDummy As Double 'button is pressed, whether the detection process If blnProcessing Then' if being processed is canceled blnProcessing = False Else Command1.Caption = "Cancel" blnProcessing = True LNGCT = 0 'Performs a million floating point multiplication calculation. After each thousand times, the detection is to be canceled. Do While BLNPROCESSING AND (LNGCT <1000000) for intYieldct = 1 to 1000 LNGCT = LNGCT 1 DBLDUMMY = LNGCT * 3.14159 Next int IELDCT 'DOEVENTS statement allows other events, including the second pressing button. Doevents loop blnprocessing = false Command1.caption = "process" MSGBOX LNGCT & "MULTIPLICATIONS WERE Performed" end iFend Sub

DoEvents switches control to the operating environment kernel. As long as all applications in this environment have the opportunity to respond to the procedure, the application will resume control. This will not give the application to discard the focus, but will make the background event to be processed. This compromise result may not always reach the expected goal. For example, the following Click event code will wait for ten seconds after the click button, and then a message is displayed. If you click on it during the button, click Operation in the reverse order.

Private submmand2_click () Static INTCLICK AS INTEGER DIM DBLICKNUMBER AS INTEGER DIM DBLENDTIME AS DOUBLE 'gives a unique value each time you click the button. INTCLICK = INTCLICK 1 INTClickNumber = INTCLICK 'Wait for ten seconds. DblendTime = Timer 10 # do while dblendtime> Timer 'does not do anything, only allows' other applications to process' their events. Doevents loop msgbox "Click" & IntClickNumber & "IS Finished" end SUB

For event processes that abandon control through Doevents, sometimes it may be desirable to prevent this process from re-calling before DOEvents returns. Otherwise, the process will not be called endlessly until the system resources are exhausted. Controls can be temporarily disabled, or like a static "logo" variable, using a static "logo" variable to prevent this.

Avoid doevents when using global data When a function has been discarded by doevents, the function can be called quite safely. For example, the next process will detect the prime number and start other application processing events with the Doevents statement:

Function PrimeStatus (TestVal As Long) As Integer Dim Lim As Integer PrimeStatus = True Lim = Sqr (TestVal) For I = 2 To Lim If TestVal Mod I = 0 Then PrimeStatus = False Exit For End If If I Mod 200 = 0 Then DoEvents Next IEND FUNCTION

A DOEVENTS statement is called every time you repeat 200 times. In this way, when the remainder of the environment responds to the event, as long as it is necessary, the primestatus process can continue to calculate.

Consider what happened during DOEvents. The application code will be suspended when other forms and applications handle events. One of these events may be a button clicking action, which will start the Primestatus process again.

This will result in re-enter the primestatus process, but because the stack is assigned a space for its parameters and local variables when the function is in the event, the re-entry does not cause conflicts. Of course, if primeStatus is used too much, "Overflow Stack Space" error may appear.

If PrimeStatus uses or changes the module-level variable or global data, the situation will be completely different. At this point, another example of the primeStatus can be executed before DOEvents, which will cause the value of module data or global data to be completely different from those values ​​prior to DOEVENTS. Thus, the results of PrimeStatus will be difficult to expect. The above code from: the source code database (SourceDataBase) Current Version: 1.0.392 Author: Shawls profile: Http://Shawls.Yeah.Net E-Mail: ShawFile@163.Net QQ: 9181729

转载请注明原文地址:https://www.9cbs.com/read-4848.html

New Post(0)