INTRAWEB下的多线程问题,困惑(100分)

  • 主题发起人 主题发起人 xwm21cn
  • 开始时间 开始时间
X

xwm21cn

Unregistered / Unconfirmed
GUEST, unregistred user!
在DELPHI的INTRAWEB程序中,建立一个子线程,但子线程需要访问主线程的东西,比如需要做“FormMain.IWTimer1.Enabled := false;”,我用Synchronize方法,但FormMain = nil,请大侠支招!十分感谢!!
下面是程序片断:

TClientThread = class(TThread)
private
sBuffer: BufferType;
// Private declarations
protected
procedure GiveAnswer;
public
procedure Execute;
override;
end;



procedure TClientThread.GiveAnswer;
begin
FormMain.IWTimerSubmit.Enabled := false;
// FormMain 为nil,故会报错
FormMain.IWLabel1.Caption := 'test';
end;


Procedure TClientThread.Execute;
begin
FreeOnTerminate := True;
Priority := tpNormal;
While (not Terminated)do
begin
Synchronize(GiveAnswer);
end;
end;

 
没做过INTRAWEB。
不过从常识来看,你的问题是线程创建早了,FormMain还没有Create你的线程就运行了。
 
谢谢Another_eYes的回复!
但我运行线程是在OnCreate里运行的,即使使其运行推迟到按某一按钮之后,该问题还是一样的,是不是intraweb不支持这种方法呢?
 
TClientThread创建时将FormMain做参数传入, (TClientThread.Create(self) //用self, 不要用FormMain)
TClientThread = class(TThread)
private
sBuffer: BufferType;
// Private declarations
protected
procedure GiveAnswer;
public
procedure Execute;
override;
constructor Create(AQueryForm: TFormMain);
end;

constructor TClientThread.Create(AQueryForm: TFormMain);
begin
QueryForm := AQueryForm;
inherited Create(False);
end;

procedure TClientThread.GiveAnswer;
begin
QueryForm.IWTimerSubmit.Enabled := false;

QueryForm.IWLabel1.Caption := 'test';
end;
 
谢谢xiao78,你真的帮了我的大忙,Another_eYes也是个热心的人,一并谢谢!
 
后退
顶部