Re: Can't use TComponent in thread object?
- From: Nicholas Sherlock <N.sherlock@xxxxxxxxx>
- Date: Fri, 24 Nov 2006 15:23:33 +1300
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
.
- Follow-Ups:
- Re: Can't use TComponent in thread object?
- From: brett
- Re: Can't use TComponent in thread object?
- References:
- Can't use TComponent in thread object?
- From: brett
- Re: Can't use TComponent in thread object?
- From: Jamie
- Re: Can't use TComponent in thread object?
- From: brett
- Re: Can't use TComponent in thread object?
- From: Rob Kennedy
- Re: Can't use TComponent in thread object?
- From: brett
- Re: Can't use TComponent in thread object?
- From: Nicholas Sherlock
- Re: Can't use TComponent in thread object?
- From: brett
- Can't use TComponent in thread object?
- Prev by Date: Re: Can't use TComponent in thread object?
- Next by Date: Re: Can't use TComponent in thread object?
- Previous by thread: Re: Can't use TComponent in thread object?
- Next by thread: Re: Can't use TComponent in thread object?
- Index(es):
Relevant Pages
|