(*----下面這個(gè)程序介紹了我們?cè)谑褂镁€(xiàn)程及未使用線(xiàn)程二種情況下,運(yùn)行該程序的反
應(yīng)。當(dāng)點(diǎn)UsedThread按鈕時(shí),則建立一個(gè)線(xiàn)程,這時(shí)我們可以在程序進(jìn)行計(jì)算的同
時(shí),改變窗體的尺寸及移動(dòng)它。當(dāng)按下NoUsedThread按鈕時(shí),不建立線(xiàn)程,我們會(huì)發(fā)
現(xiàn)在程序沒(méi)有計(jì)算完之前根本不能做其它任何事情!
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;//定義一個(gè)句柄
ThreadID:DWord;
begin
//創(chuàng)建線(xiàn)程,同時(shí)線(xiàn)程函數(shù)被調(diào)用
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);
//沒(méi)有創(chuàng)建線(xiàn)程時(shí),直接調(diào)用線(xiàn)程函數(shù)
end;
end.
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注