(*---The following program introduces the reverse of running the program when we use threads and without threads.
answer. When clicking the UsedThread button, a thread is created. At this time, we can perform calculations in the program.
When changing the size of the form and moving it. When the NoUsedThread button is pressed, if the thread is not created, we will send
Now you can't do anything else before the program is 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);
end;
procedure TForm1.UsedThreadClick(Sender: TObject);
var
hThread:Thandle;//Define a handle
ThreadID:DWord;
Begin
//Create a thread, and the thread function is called at the same time
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);
//When no thread is created, the thread function is called directly
end;
end.