R
redted3
Unregistered / Unconfirmed
GUEST, unregistred user!
线程中数组控件的问题,代码如下,你运行几次后就出现'Access violation at ...'错误
,请各位高手帮我,先谢谢了
Form1是主form
procedure TForm1.Button1Click(Sender: TObject);
begin
Form2:=TForm2.Create(nil);
Try
Form2.SHowModal;
FInally
Form2.free;
Form2:=nil;
end;
end;
form2测试线程
uses Thrdu;
{$R *.dfm}
var
MyThread:TTestThread;
procedure TForm2.BtnCloseClick(Sender: TObject);
begin
close;
end;
procedure TForm2.BtnTestThreadClick(Sender: TObject);
begin
if ThreadList.count<=0 then
begin
try
MyThread:=TTestThread.Create(False);
ThreadList.Add(self);
Except
MyThread.Terminate;
end;
end;
MyThread.Priority:=tpNormal;
MyThread.CtrlEdit[0]:=Edit1 As TEdit;
MyThread.CtrlEdit[1]:=Edit2 As TEdit;
end;
procedure TForm2.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
if ThreadList.Count>0 then
begin
MyThread.terminate;
end;
end;
procedure TForm2.FormCreate(Sender: TObject);
begin
ThreadList:=TList.Create;
end;
end.
线程单元Thrdu
unit Thrdu;
interface
uses
Classes,SysUtils,StdCtrls;
type
TTestThread = class(TThread)
private
Answer:Integer;
public
CtrlEdit:Array[0..1] of TEdit;
protected
procedure CtrlEditEnabled;
procedure Execute;
override;
end;
var
ThreadList:TList;
//因为该线程是通用的,要在几个form中调用,所以我就定义这里了
implementation
procedure TTestThread. CtrlEditEnabled;
begin
(CtrlEdit[0] As TEdit).Enabled:=False;
(CtrlEdit[1] As TEdit).Enabled:=False;
end;
procedure TTestThread.Execute;
var
i:integer;
begin
try
FreeOnTerminate:=True;
for i:=1 to 200000000do
begin
if Terminated then
break;
Inc(Answer,Round(Abs(Sin(Sqrt(I)))));
CtrlEdit[0].Text:=IntToStr(Answer);
end;
Finally
ThreadList.Pack;
ThreadList.Count:=0;
//(CtrlEdit[0] As TEdit).Enabled:=False;
//(CtrlEdit[1] As TEdit).Enabled:=False;
Synchronize(CtrlEditEnabled);
//错误就在这里和上面两行,如果我去掉这行
//就没有问题
end;
end;
end.
,请各位高手帮我,先谢谢了
Form1是主form
procedure TForm1.Button1Click(Sender: TObject);
begin
Form2:=TForm2.Create(nil);
Try
Form2.SHowModal;
FInally
Form2.free;
Form2:=nil;
end;
end;
form2测试线程
uses Thrdu;
{$R *.dfm}
var
MyThread:TTestThread;
procedure TForm2.BtnCloseClick(Sender: TObject);
begin
close;
end;
procedure TForm2.BtnTestThreadClick(Sender: TObject);
begin
if ThreadList.count<=0 then
begin
try
MyThread:=TTestThread.Create(False);
ThreadList.Add(self);
Except
MyThread.Terminate;
end;
end;
MyThread.Priority:=tpNormal;
MyThread.CtrlEdit[0]:=Edit1 As TEdit;
MyThread.CtrlEdit[1]:=Edit2 As TEdit;
end;
procedure TForm2.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
if ThreadList.Count>0 then
begin
MyThread.terminate;
end;
end;
procedure TForm2.FormCreate(Sender: TObject);
begin
ThreadList:=TList.Create;
end;
end.
线程单元Thrdu
unit Thrdu;
interface
uses
Classes,SysUtils,StdCtrls;
type
TTestThread = class(TThread)
private
Answer:Integer;
public
CtrlEdit:Array[0..1] of TEdit;
protected
procedure CtrlEditEnabled;
procedure Execute;
override;
end;
var
ThreadList:TList;
//因为该线程是通用的,要在几个form中调用,所以我就定义这里了
implementation
procedure TTestThread. CtrlEditEnabled;
begin
(CtrlEdit[0] As TEdit).Enabled:=False;
(CtrlEdit[1] As TEdit).Enabled:=False;
end;
procedure TTestThread.Execute;
var
i:integer;
begin
try
FreeOnTerminate:=True;
for i:=1 to 200000000do
begin
if Terminated then
break;
Inc(Answer,Round(Abs(Sin(Sqrt(I)))));
CtrlEdit[0].Text:=IntToStr(Answer);
end;
Finally
ThreadList.Pack;
ThreadList.Count:=0;
//(CtrlEdit[0] As TEdit).Enabled:=False;
//(CtrlEdit[1] As TEdit).Enabled:=False;
Synchronize(CtrlEditEnabled);
//错误就在这里和上面两行,如果我去掉这行
//就没有问题
end;
end;
end.