创建线程的填空题(牛人请进) ( 积分: 50 )

  • 主题发起人 主题发起人 gzccjz
  • 开始时间 开始时间
G

gzccjz

Unregistered / Unconfirmed
GUEST, unregistred user!
CreateThread参数是怎样的?
有一函数如下;
function MyThreadFunc(P:pointer;x:integer):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,x,x,Pchar(S),length(S));
end;
ReleaseDC(Form1.Handle,DC);
end;

如何用CreateThread传两个参数给MyThreadFunc来创建线程?
hthread:=CreateThread(nil,0,@MyThreadfunc,nil,0,ThreadID); 这句只传了一个参数,如何加第二个??
 
CreateThread()函数中的第四个参数是用来传递线程函数的参数的,该参数被定义为Pointer类型,所以你需要给线程函数传入一个指针。这样你需要把你的线程函数的参数定义成一个指针结构来传递实参。
写成这样就可以了:
unit Unitaaa;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
hthread : THandle;
type
PMyPointer = ^TMyPointer;
TMyPointer = record
x,y : integer;
end;
var
MyPointer : PMyPointer;
implementation

{$R *.dfm}

function MyThreadFunc(thePointer : PMyPointer):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,thePointer^.x,thePointer^.y,Pchar(S),length(S));
end;
ReleaseDC(Form1.Handle,DC);
ExitThread(0);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
ThreadID : DWORD;
begin
New(MyPointer);
MyPointer^.x := 10;
MyPointer^.y := 20;
hthread := CreateThread(nil,0,@MyThreadfunc,MyPointer,0,ThreadID);
Dispose(MyPointer);
end;

end.
 
thank you .根据二楼的代码写了一个程序,在Button1Click 按钮,我按第一次产生第一个线程,还未执行完,再按第二次却出错.为何?
 
把 MyPointer 变成局部变量
 
不要用CreateThread,要用BeginThread。。。。
 
DC:=GetDC(Form1.Handle);
for i:=0 to 500000 do
begin
S:=Inttostr(i);
Textout(DC,thePointer^.x,thePointer^.y,Pchar(S),length(S));
end;
ReleaseDC(Form1.Handle,DC);
ExitThread(0);
这里应该使用线程互斥对象,比如entercriticalsection,leavecriticalsection
 
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons;
type
TForm1 = class(TForm)

BitBtn1: TBitBtn;
BitBtn2: TBitBtn;
BitBtn3: TBitBtn;
Button1: TButton;
Edit1: TEdit;

procedure BitBtn1Click(Sender: TObject);
procedure BitBtn2Click(Sender: TObject);
procedure BitBtn3Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
hthread : THandle;
type
PMyPointer = ^TMyPointer;
TMyPointer = record
x,y : integer;
end;

implementation
{$R *.dfm}
function MyThreadFunc(P:PMyPointer):Longint;stdcall;
var
i:longint;
DC:HDC;
S:string;
begin
DC:=GetDC(Form1.Handle);
for i:=0 to 5000000 do begin
S:=Inttostr(i);
Textout(DC,p.x,p.y,Pchar(S),length(S));
end;
ReleaseDC(Form1.Handle,DC);
ExitThread(0);

end;

procedure TForm1.BitBtn1Click(Sender: TObject);

var
hThread:Thandle;//定义一个句柄
ThreadID:DWord;
MyPointer : PMyPointer;
begin
New(MyPointer);
MyPointer^.x := strtoint(edit1.text);
MyPointer^.y := strtoint(edit1.text);
//创建线程,同时线程函数被调用
hthread:=BeginThread(nil,0,@MyThreadfunc,MyPointer,0,ThreadID);
if hThread=0 then
end;

//如楼上几位所言,改进了程序,但还是button1只能按一次???
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
681
import
I
I
回复
0
查看
585
import
I
后退
顶部