小
小度
Unregistered / Unconfirmed
GUEST, unregistred user!
请看下面的代码:
unit Main;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TMainForm = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
MainForm: TMainForm;
implementation
{$R *.DFM}
{ NOTE: Change GlobalStr from var to threadvar to see difference }
//var
threadvar //请注意这里的全局变量类型:VAR 与 THREADVAR 的不同?????????
GlobalStr: string;
type
TTLSThread = class(TThread)
private
FNewStr: String;
protected
procedure Execute;
override;
public
constructor Create(const ANewStr: String);
end;
procedure SetShowStr(const S: String);
begin
if S = '' then
MessageBox(0, PChar(GlobalStr), 'The string is...', MB_OK)
else
GlobalStr := S;
end;
constructor TTLSThread.Create(const ANewStr: String);
begin
FNewStr := ANewStr;
inherited Create(False);
end;
procedure TTLSThread.Execute;
begin
FreeOnTerminate := True;
SetShowStr(FNewStr);
SetShowStr('');
end;
procedure TMainForm.Button1Click(Sender: TObject);
begin
SetShowStr('Hello world');
//设置变量值
SetShowStr('');
//显示变量值;
TTLSThread.Create('Dilbert');
Sleep(2000);
//SLEEP 函数在这里到底起什么作用呢??????????
SetShowStr('');
end;
end.
在以上代码中,改变全局变量GLOBALSTR 的类型会出现不同的效果!!但注销
。。。。。
Sleep(2000);
//SLEEP 函数在这里到底起什么作用呢??????????
。。。。。。。
这行之后的结果和运用THREADVAR或VAR类型变量的结果相同,为什么?????它在这里起什么作用呢?????
unit Main;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TMainForm = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
MainForm: TMainForm;
implementation
{$R *.DFM}
{ NOTE: Change GlobalStr from var to threadvar to see difference }
//var
threadvar //请注意这里的全局变量类型:VAR 与 THREADVAR 的不同?????????
GlobalStr: string;
type
TTLSThread = class(TThread)
private
FNewStr: String;
protected
procedure Execute;
override;
public
constructor Create(const ANewStr: String);
end;
procedure SetShowStr(const S: String);
begin
if S = '' then
MessageBox(0, PChar(GlobalStr), 'The string is...', MB_OK)
else
GlobalStr := S;
end;
constructor TTLSThread.Create(const ANewStr: String);
begin
FNewStr := ANewStr;
inherited Create(False);
end;
procedure TTLSThread.Execute;
begin
FreeOnTerminate := True;
SetShowStr(FNewStr);
SetShowStr('');
end;
procedure TMainForm.Button1Click(Sender: TObject);
begin
SetShowStr('Hello world');
//设置变量值
SetShowStr('');
//显示变量值;
TTLSThread.Create('Dilbert');
Sleep(2000);
//SLEEP 函数在这里到底起什么作用呢??????????
SetShowStr('');
end;
end.
在以上代码中,改变全局变量GLOBALSTR 的类型会出现不同的效果!!但注销
。。。。。
Sleep(2000);
//SLEEP 函数在这里到底起什么作用呢??????????
。。。。。。。
这行之后的结果和运用THREADVAR或VAR类型变量的结果相同,为什么?????它在这里起什么作用呢?????