****[线程私有变量丢失的问题]**** ( 积分: 100 )

P

prtmon

Unregistered / Unconfirmed
GUEST, unregistred user!
线程类
type
TPrcDataThread = class(TThread)
private
PrcNo:Integer;
//私有变量
protected
procedure Execute;
override;
public
constructor Create(CommNo:Integer);
end;

线程建立
constructor TPrcDataThread.Create(CommNo:Integer);
begin
inherited Create(True);
PrcNo:=CommNo;
FreeOnTerminate:=True;
Priority:=tpLowest;
end;
procedure TPrcDataThread.Execute;
在这里调用PrcNo,发现此变量经常读不到值,为什么啊?
 
你把Create定义成虚函数试试
 
你咋调用的?
 
constructor TPrcDataThread.Create(CommNo:Integer);
begin
PrcNo:=CommNo;
FreeOnTerminate:=True;
Priority:=tpLowest;
[red]inherited Create(True);[/red]
end;

inherited放最后面啊!否则Create后,立刻其新线程执行了Execute方法了,而这个时候你再给ProcNo赋值的操作可能已经晚了!
 
我已经找到原因了,在对另外一个array of char变量操作时,没有设置长度,引起线程出错,线程出错后就丢失了变量值了
 
多人接受答案了。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
547
import
I
顶部