创建子线程,哪错了(10分)

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

angel725

Unregistered / Unconfirmed
GUEST, unregistred user!
unit Unit1;<br><br>interface<br><br>uses<br> &nbsp;Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> &nbsp;Dialogs, StdCtrls;<br><br>type<br> &nbsp;TForm1 = class(TForm)<br> &nbsp; &nbsp;Button1: TButton;<br> &nbsp; &nbsp;Edit1: TEdit;<br> &nbsp; &nbsp;procedure Button1Click(Sender: TObject);<br> &nbsp;private<br> &nbsp; &nbsp;{ Private declarations }<br> &nbsp;public<br> &nbsp; &nbsp;{ Public declarations }<br> &nbsp; &nbsp;function ss():integer;<br> &nbsp;end;<br><br>var<br> &nbsp;Form1: TForm1;<br><br>implementation<br><br>{$R *.dfm}<br><br>function tform1.ss ():integer;<br> &nbsp;begin<br> &nbsp; &nbsp;edit1.Text:='33';<br> &nbsp;end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br> &nbsp;mythead:thandle;<br> &nbsp;id:dword;<br>begin<br> &nbsp; mythead:=createthread(nil,0,@ss,nil,0,id);<br>end;<br><br>end.<br><br>错误:<br>Build<br> &nbsp;[Warning] Unit1.pas(31): Return value of function 'TForm1.ss' might be undefined<br> &nbsp;[Error] Unit1.pas(38): Variable required<br> &nbsp;[Fatal Error] Project1.dpr(5): Could not compile used unit 'Unit1.pas'
 
ss函数没有返回值
 
function ss(p: pointer): cardinal;stdcall;<br>begin<br>end;<br>var<br> id: Cardinal;<br>CreateThread(nil, 0, @ss, 0, id);
 
unit Unit1;<br><br>interface<br><br>uses<br> &nbsp;Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> &nbsp;Dialogs, StdCtrls;<br><br>type<br> &nbsp;TForm1 = class(TForm)<br> &nbsp; &nbsp;Button1: TButton;<br> &nbsp; &nbsp;Edit1: TEdit;<br> &nbsp; &nbsp;procedure Button1Click(Sender: TObject);<br> &nbsp;private<br> &nbsp; &nbsp;{ Private declarations }<br> &nbsp;public<br> &nbsp; &nbsp;{ Public declarations }<br> &nbsp; &nbsp;function ss (p: pointer): cardinal;stdcall;<br> &nbsp;end;<br><br>var<br> &nbsp;Form1: TForm1;<br><br>implementation<br><br>{$R *.dfm}<br><br>function tform1.ss (p: pointer): cardinal;stdcall;<br> &nbsp;begin<br> &nbsp; &nbsp;edit1.Text:='33';<br> &nbsp;end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br> &nbsp;mythead:thandle;<br> &nbsp;id:Cardinal;<br>begin<br> &nbsp; &nbsp; mythead:=CreateThread(nil,0,@ss,0,id);<br> // &nbsp;mythead:=createthread(nil,0,@ss,0,0,id);<br>end;<br><br>end.<br><br>这样的,还是一样的错误,
 
ss要独立出来,不能是类方法
 
嗯???要怎么独立出来?
 
不是独立 是必须以<br>DWORD WINAPI ThreadFunc( LPVOID ) 这种格式传入
 
不是很明白,我要改哪个地方<br>怎么改呢?
 
createthread 的第三个参数[8D]
 
唉,看了这么多回贴,都错了。<br>基础学习要加强啊,什么时候合适用fun什么时候合适用pro<br><br>unit Unit1;<br><br>interface<br><br>uses<br> &nbsp;Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> &nbsp;Dialogs, StdCtrls;<br><br>type<br> &nbsp;TForm1 = class(TForm)<br> &nbsp; &nbsp;Button1: TButton;<br> &nbsp; &nbsp;Edit1: TEdit;<br> &nbsp; &nbsp;procedure Button1Click(Sender: TObject);<br> &nbsp;private<br> &nbsp; &nbsp;{ Private declarations }<br> &nbsp;public<br> &nbsp; &nbsp;{ Public declarations }<br><br> &nbsp;end;<br><br>var<br> &nbsp;Form1: TForm1;<br><br>implementation<br><br>{$R *.dfm}<br><br>procedure ss;<br> &nbsp;begin<br> &nbsp;form1.Edit1.Text:='33';<br> &nbsp;end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br> &nbsp;mythead:thandle;<br> &nbsp;id:dword;<br>begin<br> &nbsp; mythead:=createthread(nil,0,@ss,nil,0,id);<br>end;<br><br>end.
 
没有分????
 
就应该用function才对,应该仔细理解一下关于createthread的帮助。<br>不在于是否要把ss提出来,不提出来也可以,看看我这样写也是可以的:<br>unit Unit1;<br><br>interface<br><br>uses<br> &nbsp;Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> &nbsp;Dialogs, StdCtrls, ExtCtrls;<br><br>type<br> &nbsp;TForm1 = class(TForm)<br> &nbsp; &nbsp;Button1: TButton;<br> &nbsp; &nbsp;Timer1: TTimer;<br> &nbsp; &nbsp;Label1: TLabel;<br> &nbsp; &nbsp;procedure Button1Click(Sender: TObject);<br> &nbsp; &nbsp;procedure Timer1Timer(Sender: TObject);<br> &nbsp;private<br> &nbsp; &nbsp;{ Private declarations }<br> &nbsp;public<br> &nbsp; &nbsp;{ Public declarations }<br> &nbsp; &nbsp;class function ss(v: pointer): integer; stdcall;<br> &nbsp;end;<br><br>var<br> &nbsp;Form1: TForm1;<br> &nbsp;b: Integer = 10;<br>implementation<br><br>{$R *.dfm}<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br> &nbsp;//mythead:thandle;<br> &nbsp;id:dword;<br>begin<br> &nbsp; {mythead := }createthread(nil, 0, @TForm1.ss, nil, 0, id);<br>end;<br><br>class function TForm1.ss(v: pointer): integer;<br>begin<br> &nbsp;b := 100;<br> &nbsp;Result := 0;<br>end;<br><br>procedure TForm1.Timer1Timer(Sender: TObject);<br>begin<br> &nbsp;Label1.Caption := IntToStr(b);<br>end;<br><br>end.<br><br>你把ss提出来也是可以的。
 
后退
顶部