关于createthead()函数建线程的一个问题 ( 积分: 100 )

  • 主题发起人 主题发起人 adomy
  • 开始时间 开始时间
A

adomy

Unregistered / Unconfirmed
GUEST, unregistred user!
调试程序一:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation
{$R *.dfm}
function ThreadFunc():Integer;stdcall;
var
count:Integer;
begin
for count:=0 to 10000do
begin
Form1.Edit1.Text:=IntToStr(count);
end;
Form1.Edit1.Text:='线程结束';
Sleep(1000);
ExitThread(5);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
Theadid1:DWORD;
begin
CreateThread(nil,0,@threadfunc,nil,0,Theadid1);
end;

end.
一个简单的调试程序编译通过
调试程序二:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
function ThreadFunc():Integer;stdcall;{ Public declarations }
end;

var
Form1: TForm1;
implementation
{$R *.dfm}
function ThreadFunc():Integer;stdcall;
var
count:Integer;
begin
for count:=0 to 10000do
begin
Form1.Edit1.Text:=IntToStr(count);
end;
Form1.Edit1.Text:='线程结束';
Sleep(1000);
ExitThread(5);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
Theadid1:DWORD;
begin
CreateThread(nil,0,@threadfunc,nil,0,Theadid1);
end;

end
编译出错:出错信息:variable required
这是为什么呢?其中涉及到了些什么问题呢!我想大家也一定遇到过吧!能不能谈谈解释下呢?
 
调试程序一:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation
{$R *.dfm}
function ThreadFunc():Integer;stdcall;
var
count:Integer;
begin
for count:=0 to 10000do
begin
Form1.Edit1.Text:=IntToStr(count);
end;
Form1.Edit1.Text:='线程结束';
Sleep(1000);
ExitThread(5);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
Theadid1:DWORD;
begin
CreateThread(nil,0,@threadfunc,nil,0,Theadid1);
end;

end.
一个简单的调试程序编译通过
调试程序二:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
function ThreadFunc():Integer;stdcall;{ Public declarations }
end;

var
Form1: TForm1;
implementation
{$R *.dfm}
function ThreadFunc():Integer;stdcall;
var
count:Integer;
begin
for count:=0 to 10000do
begin
Form1.Edit1.Text:=IntToStr(count);
end;
Form1.Edit1.Text:='线程结束';
Sleep(1000);
ExitThread(5);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
Theadid1:DWORD;
begin
CreateThread(nil,0,@threadfunc,nil,0,Theadid1);
end;

end
编译出错:出错信息:variable required
这是为什么呢?其中涉及到了些什么问题呢!我想大家也一定遇到过吧!能不能谈谈解释下呢?
 
public
function ThreadFunc():Integer;stdcall;{ Public declarations }
在哪里实现的?另外,CreateThread要求的是一个纯粹的stdcall调用约定的函数,Form的方法有另外的隐含参数Self,哪里能够作为CreateThread的参数?
 
public
function ThreadFunc():Integer;stdcall;{ Public declarations }
这里不行吧,StdCall用在这里怎么不会出错呢?
 
??你试试看不用stdcall也是会出错的,有人说是线程过程应该是一个全局变量!不能定义在类中!
 
多人接受答案了。
 
后退
顶部