16,748
社区成员
发帖
与我相关
我的任务
分享
unit Unit3;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm3 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
procedure FormProc; {准备给线程使用的方法}
end;
var
Form3: TForm3;
implementation
{$R *.dfm}
var
hThread: THandle;
{线程入口函数}
function MyThreadFun(p: Pointer): DWORD;
begin
Form3.FormProc; {调用 TForm1 类的方法}
Result := 99; {这个返回值将成为线程的退出代码, 99 是我随意给的数字}
end;
{TForm1 的方法, 本例中是给线程的入口函数调用的}
procedure TForm3.FormProc;
var
i: Integer;
begin
for i := 0 to 200000 do
begin
with Form3.Canvas do begin
Lock;
TextOut(10, 10, IntToStr(i));
Unlock;
end;
end;
end;
{建立并执行线程}
procedure TForm3.Button1Click(Sender: TObject);
var
ID: DWORD;
begin
hThread := CreateThread(nil, 0, @MyThreadFun, nil, 0, ID);
end;
{获取线程的退出代码, 并判断线程是否退出}
procedure TForm3.Button2Click(Sender: TObject);
var
ExitCode: DWORD;
begin
GetExitCodeThread(hThread, ExitCode);
if hThread = 0 then
begin
Text := '线程还未启动';
Exit;
end;
if ExitCode = STILL_ACTIVE then
Text := Format('线程退出代码是: %d, 表示线程还未退出', [ExitCode])
else
Text := Format('线程已退出, 退出代码是: %d', [ExitCode]);
end;
end.
procedure fcs;
begin
with TForm3.Create(nil) do
begin
Show;
end;
end;
发现应该这么写
procedure fcs;
begin
Form3:=TForm3.Create(nil);
Form3.Show;
end;
感谢你的点拨,让我恍然大悟。