线程函数的调用(100分)

L

lizheng

Unregistered / Unconfirmed
GUEST, unregistred user!
function ThreadFunc(P: Pointer): LongInt;
stdcall;
var
i: Integer;
DC: HDC;
S: string;
begin
DC := GetDC(Form1.Handle);
SetBkColor(DC, GetSysColor(color_btnface));
for i := 0 to 100000do
begin
S := IntToStr(i);
TextOut(DC, 10, 10, PChar(S), Length(S));
end;
ReleaseDC(Form1.Handle, DC);
end;

procedure TForm1.FindFile1Click(Sender: TObject);
label show;
var
hThread:THandle;
ThreadID:DWord;
path:pointer;
begin

show:
form3.ShowModal;
if form3.ModalResult=mrok then

if (form3.Edit1.Text='') or (form3.Edit2.Text='') then
begin

messagebeep(MB_ICONEXCLAMATION);
messagedlg('something necessary you did not fill',mterror,[mbok],0);
goto show;
end
else
begin

datamodule2.table2.Append;

datamodule2.table2.FieldByName('光盘编号').asinteger:=strtoint(form3.edit2.text);
datamodule2.table2.FieldByName('光盘名称').asstring:=form3.edit1.text;
path:=Pointer(new(Ppath));
Ppath(path)^.path:=form3.DriveComboBox1.Drive+':/';
Hthread:=CreateThread(nil,0,@ThreadFunc,path,0,ThreadID);
~~~~~~
出错:Undeclared Identifier?
if hthread=0 then
messageBox(Handle,'no Thread',nil,MB_OK);
//form1.FindFile(form3.DriveComboBox1.Drive+':/');
end;

end;

可下面程序中的调用是对的:

procedure TForm1.bUseThreadClick(Sender: TObject);
var
hThread: THandle;
ThreadID: DWord;
begin
hthread := CreateThread(nil, //Security attribute
0, //Initial Stack
@ThreadFunc, //Starting address of thread
nil, // argument of thread
0, // Create flags
ThreadID);
// thread ID
if hthread = 0 then
MessageBox(Handle, 'No Thread', nil, MB_OK);
end;
调用的是同样的函数,一个编译通过,一个不行(虽然是在不同的程序中)。想不通?
 
把 path定义为指向字符串的指针,然后
path^:=form3.DriveComboBox1.Drive+':/';
在执行试试.
注:小弟我只是Delphi小学徒,如果上述方法不能奏效,
请不要笑我呀! 嘻嘻!!!
 
创建线程时,调用的可不可以是过程,因为有时我不给返回值,编译时会有warning,
to 探索者:
  好象不行,否则我也不用定义一个结构了.
 
Hthread:=System.CreateThread(nil,0,@ThreadFunc,path,0,ThreadID);
 
unit thread;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Menus, Grids, DBGrids;

type
Ppath=^Tpath;
Tpath=record
path:string;
end;
TForm1 = class(TForm)
DBGrid1: TDBGrid;
DBGrid2: TDBGrid;
MainMenu1: TMainMenu;
File1: TMenuItem;
FindFile1: TMenuItem;
procedure FindFile1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
wenjianNO:integer;
implementation
uses thread1;
//datamodule2
{$R *.DFM}
procedure TForm1.FindFile1Click(Sender: TObject);
var
hThread:THandle;
ThreadID:DWord;
path:pointer;
begin
path:=Pointer(new(Ppath));
Ppath(path)^.path:='h:/';
Hthread:=CreateThread(nil,0,@FindFile,path,0,ThreadID);
> ~~~~~Undeclared Identifier
if hthread=0 then
messageBox(Handle,'no Thread',nil,MB_OK);
end;

function FindFile(path:pointer):Longint;stdcall;
var
SearchRec: TSearchRec;
path1:pointer;
err:integer;
begin
try
err:=FindFirst(Ppath(path)^.path+'*.*',faAnyFile, SearchRec);
while err = 0do
begin
if SearchRec.Name[1]<>'.' then
begin
if ((SearchRec.Attr and fadirectory)>0) AND (SearchRec.Name[1]<>'.') then
begin
datamodule2.table1.Append;
datamodule2.table1.FieldByName('文件编号').asinteger:=wenjianNO;
datamodule2.table1.FieldByName('文件名称').asstring:=searchrec.Name;
datamodule2.table1.FieldByName('文件类型').asstring:='文件夹';
datamodule2.table1.Post;
wenjianNO:=wenjianNO+1;
try
path1:=Pointer(new(Ppath));
Ppath(path1)^.path:=Ppath(path)^.path+String(SearchRec.Name)+'/';
FindFile(path1);
except
exit;
end;
end
else
begin
datamodule2.table1.Append;
datamodule2.table1.FieldByName('文件编号').asinteger:=wenjianNO;
datamodule2.table1.FieldByName('文件名称').asstring:=searchrec.Name;
datamodule2.table1.FieldByName('文件类型').asstring:=ExtractFileExt(SearchRec.Name);
datamodule2.table1.FieldByName('所属文件夹').asstring:=Ppath(path)^.path;
datamodule2.table1.Post;
wenjianNO:=wenjianNO+1;
end;
end;
err:= FindNext(SearchRec);
end;
finally
FindClose(SearchRec);
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
wenjianNO:=1;
end;

end.

真是不好意思,我总是问题多多,还烦打下帮帮忙。
此问题和上次是一样的,
而且我想知道,在什么情况下,调用线程函数时,会出现这种错误。
 
function FindFile(path:pointer):Longint;stdcall;
var
Form1: TForm1;
wenjianNO:integer;
implementation
.....
你的问题是findfile在调用者之后定义了。所以调用时编译器找不到findfile的出处。
将findfile函数整个放在TForm1.FindFile1Click之前.
或者在Interface部分申明一下findfile函数(如上).
 
搞定了.
 

Similar threads

顶部