Use of Yield () function in Pb

Source: Internet
Author: User

The Yield () function is
It is a very useful function in PB program control. If you can use it properly, it will certainly make your program more user-friendly.

The Yield () function transfers control to other graphical objects, including non-PowerBuilder objects. This function detects message queues. If there is a message, the message is taken out.

This function returns a Boolean value. If a message is extracted from the message queue, the function returns TRUE; otherwise, the function returns FALSE .. You can use this function to transfer control to other applications when performing a time-consuming operation.

Normally, the PowerBuilder application does not respond to user operations when executing a piece of code (such as retrieving data or executing a loop program. For short-lived code segments, there is nothing wrong with this processing method. However, if the execution of a code segment takes a long time (such as in a large amount of data retrieval or code that is executed cyclically ), if your application wants to provide more control, you need to insert the Yield () function in this code so that you can perform other operations, for example, allows the user to press the cancel button in another window, and this button changes a global (shared) variable in Yield () in the subsequent code of the function, you can determine the value of the global (shared) variable to determine the next operation. Application Execution
After Yield () function, if a message is found in the message queue, it allows the object to process the message. After processing, the Yield () function continues to run the code.

Although you cannot perform other tasks during retrieval, when you place the Yield () function in a RetrieveRow event, you can run other applications at the same time. Of course, inserting the Yield () function in the code will reduce the running efficiency of the application.

Example 1.

The following code executes a long process. In this process, we insert the Yield () function. When you click the cancel button in the window, the execution of this Code is terminated, the shared variable sb_interrupt is used to indicate whether the user has clicked the "cancel" button:

Integer n

// Sb_interrupt is the shared variable

Sb_interrupt = FALSE

FOR n = 1 to 3000

Yield ()

IF sb_interrupt THEN

// Modify the value of sb_interrupt in the Clicked event handler of the cancel button

MessageBox ("debug", "user interrupted! ")

Sb_interrupt = FALSE

EXIT

ELSE

... // Other processing

END IF

NEXT

The Clicked event handler of the cancel button can be written as sb_interrupt = TRUE.

Example 2.

In this example, the program plays the same ield () function in the interactive frequency, and the user can control the second window, however, you cannot see the change of the window focus before the loop is completed.

Integer n

FOR n = 1 to 3000

Yield ()

... // Some processing

NEXT

Example 3.

In this example, a script wants to open a DDE channel with Lotus Notes, whose executable name is stored in the variable mailprogram. if the program isnt running, the script starts it and loops, waiting until the programs startup is finished and it can establish
A DDE channel. The loop between des Yield, so that the computer can spend time actually starting the other program:

Time starttime

Long hndl

SetPointer (HourGlass !)

// Try to establish a handle; SendMail is the topic.

Hndl = OpenChannel ("Notes", "SendMail ")

// If the program is not running, start it

IF hndl $ # @ 60; 1 then

Run (mailprogram, Minimized !)

Starttime = Now ()

// Wait up to 2 minutes for Notes to load

// And the user to log on.

DO

// Yield control occasionally.

Yield ()

// Is Notes active yet?

Hndl = OpenChannel ("Notes", "SendMail ")

// If Notes is active.

IF hndl $ # @ 62; 0 THEN EXIT

LOOP Until SecondsAfter (StartTime, Now () $ # @ 62; 120

// If 2 minutes pass without opening a channel

If hndl $ # @ 60; 1 then

MessageBox ("error ",&

"Cant start notes.", stopsign !)

Setpointer (arrow !)

Return

End if

End if

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.