(* ---- The following program introduces the reaction of running the program with thread and unused thread. When you point the usedthread button, a thread is created, then we can calculate the program. At the same time, change the size of the form and move it. When you press the Nousedthread button, do not build threads, we will find anything else before the program is not calculated!
Unit unit1;
Interface
Uses Windows, Messages, Sysutils, Variants, Classes, Graphics, Controls, Forms, Dialogs, Stdctrls, Buttons
type TForm1 = class (TForm) UsedThread: TBitBtn; NoUsedThread: TBitBtn; procedure UsedThreadClick (Sender: TObject); procedure NoUsedThreadClick (Sender: TObject); private {Private declarations} public {Public declarations} end;
Var Form1: TFORM1;
IMPLEMentation
{$ R * .dfm}
Function mythreadfunc (p: pointer): longint; stdcall; var i: longint; dc: hdc; s: string; begin dc: = getdc (form1.handle); for i: = 0 to 500000 do begin s: = intTostr I); TextOut (DC, 10, 10, PCHAR (S), Length (s)); end; releasedc (form1.handle, dc);
procedure TForm1.UsedThreadClick (Sender: TObject); var hThread: Thandle; // define a handle ThreadID: DWord; begin // create a thread, while the thread function is called hthread: = CreateThread (nil, 0, @ MyThreadfunc, nil, 0 , ThreadID; if hthread = 0 Then MessageBox (Handle, 'Didn'tcreateathread', NIL, MB_OK); END;
Procedure TFORM1.NOUSEDTHREADCLICK (Sender: TOBJECT); Begin MythreadFunc (nil); // Directly call the thread function end; end when no thread is created.