Re: Can't use TComponent in thread object?



brett wrote:
Only the main thread can work with the GUI. To work around this, check
out the "Synchronize" procedure in your help. I don't see why you need a
separate TProgressThread (But it may depend on which socket library you
are using).

Thanks Nicholas. How can Synchronize help? I still run into a circular
reference problems. My form needs a reference to the thread class to
start the thread. The thread will need a reference to the form so it
can update a label. The update label method will be called using
Synchronize in the thread's execute method.

There's no reason why the situation you describe should be impossible due to circular references - One unit can use another in the "Implementation" section if it doesn't need the unit in its "Interface" section.

In any case, I'd have your form pass the label as an argument to the thread's constructor. This way, the thread doesn't need to know anything about the form, and it makes it more reusable.

TMyThread=class(TThread)
private
proglabel:TLabel;
procedure UpdateProgress;
public
constructor create(createsuspended:boolean; progressLabel:TLabel);
end;

constructor TMyThread.create(createsuspended:boolean; progressLabel:TLabel);
begin
inherited(createsuspended);

proglabel:=progressLabel;

synchronize(UpdateProgress); //UpdateProgress must be called with syncrhonize
end;

procedure TMyThread.UpdateProgress;
begin
//here we can interact with the GUI

proglabel.caption:='50% done...';
end;

Cheers,
Nicholas Sherlock

--
http://www.sherlocksoftware.org
.



Relevant Pages

  • Re: Cant use TComponent in thread object?
    ... My form needs a reference to the thread class to ... The update label method will be called using ... Synchronize in the thread's execute method. ...
    (alt.comp.lang.borland-delphi)
  • Re: Cant use TComponent in thread object?
    ... out the "Synchronize" procedure in your help. ... My form needs a reference to the thread class to ... The update label method will be called using ...
    (alt.comp.lang.borland-delphi)
  • Re: How to make custom linked cross-references?
    ... two macros \extractrefnum and \extractpagerefnum. ... If somebody wants the package on CTAN, ... A reference to section~of this book ... You might wish to take reference to yet another label within ...
    (comp.text.tex)
  • Re: xr-hyper and includeonly
    ... Write the label to a secondary aux-file. ... a prefix in front of each labelname (e.g., ... are about to reference was defined within the current file. ...
    (comp.text.tex)
  • Re: Pointers or mutually referential values
    ... structures with elements (like AST nodes) that reference each other. ... while a statement would have a reference to its label. ... making it more elegant. ...
    (comp.lang.functional)