Multi-threaded program design under Delphi (2)

zhaozj2021-02-17  51

Multi-threaded program design under Delphi (2)

END;

END;

ProceduRetbounceThread. EXECUTE;

Begin

Whilenotterminateddo

Begin

Synchronize (moveshape);

END;

END;

Constructortbouncethread. Create (SUSPENDED: Boolean; Shape: Tshape; XSpeed, YSPEED: Integer);

Begin

InheritedCreate (SUSPENDED);

Fshape: kappashape;

FXSPEED: κxspeed; {x-axis direction speed}

Fyspeed: yspeed; {Y axis towing speed}

FreeOnterminate: κtrue;

END;

End.

This is a multi-threaded ball game, you can have multiple different balls, they belong to different threads, their independent collisions on the screen. Obviously, since the display of multiple balls will operate simultaneously, in order to be safe, we add Synchronize (MoveShape) in the executive execution section in the Execute process, in fact, wherever you need to operate VCL resources For example, a form, a bitmap, should be added to the SYNCHRONIZE call.

When executed, we can create a new program, then add the above BNCTHRD unit in the USES section, and then add two Shape controls Shape1 and Shape2 on its form FORM1, and Shape1 can be a rectangle and the Shape2 is a circle. Add the following code to make the rectangle and circle.

Proceduretform1. Button1Click (Sender: TOBJECT);

Begin

TBounceThread. Create (false, shape1, 1, 2);

TBounceThread. Create (false, shape2, 2, 3);

END;

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

New Post(0)