线程类中使用其他类的变量不能赋值
在创建了一个线程之后,需要在线程中使用一个类变量,在对这个类变量进行赋值的时候程序挂掉请问程序是什么地方有错呢?
请大牛指点指点!
下面是源码
interface
uses
Classes, SPComm, Windows, DataType, Contnrs, SyncObjs;
type
PTComm = ^Tcomm;
Thread485 = class(TThread)
private
{ Private declarations }
Comm : PTComm;
S : TStrings;
CriticalSection : TCriticalSection;
MsgQuene : TObjectQueue;
protected
procedure Execute; override;
public
constructor Create(AComm:PTComm; ASuspended : Boolean);
procedure Push(s : TStrings);
function Pop:TStrings;
function Peek:TStrings;
function MsgCount:Integer;
end;
implementation
{ Important: Methods and properties of objects in visual components can only be
used in a method called using Synchronize, for example,
Synchronize(UpdateCaption);
and UpdateCaption could look like,
procedure Thread485.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }
{ Thread485 }
constructor Thread485.Create(AComm:PTComm; ASuspended : Boolean);
begin
inherited Create(ASuspended);
FreeOnTerminate := True;
Comm := AComm;
try
Comm.StartComm;
except
Terminate;
end;
S := TStrings.Create;
Beep(1000,100);
s.Text := 'ygm ';//在这里出错挂掉
end;
procedure Thread485.Execute;
var
st : string;
begin
while not Terminated do
begin
st := s.Text;
Comm.WriteCommData(PChar(st), Length(st));
Sleep(1000);
end;
s.Free;
Comm.StopComm;
end;
procedure Thread485.Push(s:TStrings);
begin
CriticalSection.Enter;
MsgQuene.Push(s);
CriticalSection.Leave;
end;
function Thread485.Pop:TStrings;
begin
CriticalSection.Enter;
Result := TStrings(MsgQuene.Pop);
CriticalSection.Leave;
end;
function Thread485.Peek:TStrings;
begin
CriticalSection.Enter;
Result := TStrings(MsgQuene.Peek);
CriticalSection.Leave;
end;
function Thread485.MsgCount:Integer;
begin
CriticalSection.Enter;
Result := MsgQuene.Count;
CriticalSection.Leave;
end;
end.