Re: Can't use TComponent in thread object?
- From: Rob Kennedy <me3@xxxxxxxxxxx>
- Date: Thu, 23 Nov 2006 20:39:35 -0600
brett wrote:
If I want to update a label on my form that displays the bytes
downloaded so far, how is that done at a conceptual level? I know what
the code will look like to make all of that happen but conceptually, I
keep running into problems - namely circular references.
Here's what I started doing:
Form1 uses FTPthread
...and creates TFTPthread
TFTPthread uses TProgressthread
...and creates TProgressthread
Do you mean to say that you're using *two* threads just to download a file?
TProgressthread uses Form1
...and gets a reference to a string property on Form1. TProgressthread
will stay in a loop checking the filesize every 5 seconds or so. It
will sleep in between. TProgressthread is stopped/destroyed by
TFTPthread.
Does this sound like a good model?
You shouldn't need to poll the file just to find out how much you've downloaded. Your FTP code should be able to tell you that directly.
Furthermore, your progress thread shouldn't need to have a reference to any form units. If it does, then you're locked into using that progress code with only that one form.
Give your thread class an event property of the following type:
type
TProgressNotifier = procedure(Sender: TFTPThread; Progress: Integer) of object;
Declare a method with that signature in your form, and when you instantiate the thread, set its event to refer to the form's method. This is what decouples the thread from having to know about what form it's interacting with.
Every time your FTP code makes progress, have it use the Synchronize method to invoke another of its methods. In the synchronized method, call the ProgressNotifer method pointer. The second parameter should be the progress that the FTP code has made.
--
Rob
.
- 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
- 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: hexadecimal calculating: turning on the high bit
- Index(es):