在创建了一个线程之后,需要在线程中使用一个类变量,在对这个类变量进行赋值的时候程序挂掉请问程序是什么地方有错呢?请大牛指点指点!下面是源码interfaceuses
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.
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.
解决方案 »
- 求C++代码转换!100分
- 求达人解决Delphi7 Windows XP 安装问题
- 求助一段循环代码,请高手指点
- 关于DELPHI窗体KEYDOWN事件
- 很简单的问题大家都来帮忙——————在线等待!!!!
- ADOCOMMAND有没有返回值?
- 怎样打开这样的文件?
- 字符变量付值的问题!很怪!
- 高手能否给我讲解一下 这两个属性的使用
- 本人使用Listview来显示数据库中员工的信息,每一个图标代表一个员工,但是发觉当一个部门的员工超过百人以后显示图标,或者在上面查找指定员工时就会很慢!大家有什么好的方法吗?
- 用adoquery和datasource与数据库相连,用adoquery.fieldbyname('number').asstring赋值,但是提示找不到该字段
- DBGrid 中文印出乱码
Beep(1000,100);
s.Text := 'ygm ';//在这里出错挂掉 这样当然出错了,TStrings是抽象类,所以不要创建它的实例来用。把 S := TStrings.Create; 改成
S := TStringList.Create;应该就好了
S := TStringlist.Create;