两个小问题,请大哥们指点(50分)

  • 主题发起人 student_541
  • 开始时间
S

student_541

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

var
Form1: TForm1;
CriticalSection: TRTLCriticalSection;
implementation
{$R *.DFM}
function ThreadFunc(Info: Pointer): Integer;
stdcall;
var
Count,i: Integer;
begin
EnterCriticalSection(CriticalSection);
Form1.Button1.Enabled:=False;
for Count := 0 to 10000do
begin
Form1.Edit1.Text := IntToStr(Count);
end;
Form1.Edit1.Text := '线程结束';
Sleep(500);
Form1.Button1.Enabled:=true;
LeaveCriticalSection(CriticalSection);
ExitThread(4);

end;

procedure TForm1.Button1Click(Sender: TObject);
var
ThreadId1, ThreadId2: DWORD;
begin
Button1.Enabled:=false;
if CriticalSection.RecursionCount<>0 then
DeleteCriticalSection(CriticalSection);
InitializeCriticalSection(CriticalSection);
CreateThread(nil, 0, @ThreadFunc, nil, 0, ThreadId1);
CreateThread(nil, 0, @ThreadFunc, nil, 0, ThreadId2);
end;

end.

这是一本书上的源代码,我的问题是
1,我记得delphi中所有的函数或过程都要先声明,可是function ThreadFunc(Info: Pointer): Integer;
stdcall;
却没有申明?为什么?
2,我查了ExitThread函数的意思是中止线程,参数可由getexitcodethread获得,
可这里却用4代替了,我将它改为任意的值都可以,是不是这个值没有什么意义,
请大哥们指点。
 
1. 不一定要先声明的,放在Interface里面是为了让其他Unit可以调用这个过程或者方法而已,如果是在Implementation里面是为了让过程或方法实体前面的代码可以使用它。
2.帮助里面说了,你没有理解,这个4就是ExitCode, 它(4)可以由GetExitCodeThread函数
获得,也就是说4只是个返回值,不是说由GetExitCodeThread函数产生的,
GetExitCodeThread只是获得在ExitThread里面返回的值而已
 
function ThreadFunc(Info: Pointer): Integer;
stdcall;是静态方法,也就是类方法;
其在implementation下面直接实现,不需声明,但它不能调用interface部分声明的方法和变量
 
谢谢各位,对于第二个问题是否可以这样说,线程中止后在主线程中可以由getexitcodethread获得中止的线程的值即 ,并且这个值ExitThread的参数,
可以是我任意设定的。
 

Similar threads

I
回复
0
查看
554
import
I
I
回复
0
查看
637
import
I
I
回复
0
查看
732
import
I
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
顶部