Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Update a Form with data from loop? 1

Status
Not open for further replies.

joniboy123

Technical User
May 1, 2003
4
GB
Hi

Ive made a program that uses a form, it does all the calculations and displays data at the press of a button.

But some data is constantly changing. Ive got all the data in my program but how do i get my form to periodically retrieve it and display it?

Do i have to put something in a main program loop?

or do i have to have a second thread? that deals only with this??

Thanks very much for your time.
 
You can drop a TTimer onto the form and set the interval to a reasonable refresh.
The timer can then call the same function as the onClick event for the button
 
While the data is being displayed and updated i need the user to be able to use the other functions on the form.

Is this possible with TTimer?

Thanks

 
Put TTimer into another thread and yes you can.

James P. Cottingham

When a man sits with a pretty girl for an hour, it seems like a minute. But let him sit on a hot stove for a minute and it's longer than any hour. That's relativity.
[tab][tab]Albert Einstein explaining his Theory of Relativity to a group of journalists.
 
The TTimer is separate from the form interaction for the most part. As long as the timer is set to a large enough interval (remember people can't determine a noticable difference on the screen faster than half a second, or it becomes annoying), then it shouldn't interfere with the user interaction on the form, unless the updates are using up the processor. You shouldn't have any problems just dropping it on the form.

Chris
 
just one more thing....

is there a very simple way to set-up a new thread of this type??

i realise im being very lazy and could look it up but whats the most basic way of doing it??

thanks!
 
Click File->New, then select Thread Object.

Add a #include for the mainform at the top of the thread code.

In the Execute() method of the thread, add the code to update your main form.

Only update it on an interval (which a TTimer could set a flag), because if the thread runs continuously updating the main form, it will slow everything down or possibly cause errors.

TThread::Execute()
{
while (!Terminated)
{
if (Form1->FlagSet)
{
PaintForm();
Form1->FlagSet = false;
}
}
}

void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
FlagSet = true;
}

Since you're going to be using a timer anyway, might as well put the paint code in it. The only reason you would want to use a thread is if the thread has some kind of read timeout on a serial port or something that causes it's code to stop running for long periods of time or that holds the processor hostage for a lengthy time.

But if you only update your screen once a second, it would really be easier to use a TTimer. It won't be a noticable difference (unless you are updating 10,000 objects on the screen!). But it's always good experience learning the thread business. So have fun!

Chris
 
TTimer basically acts as a threaded object in my limited experience. It does not interfere with any functions that
I am doing on the form such as editing text or button clicks. Check it out without embedding the TTimer in any thread as it may already be such.

tomcruz.net
 
Ive tried to implement the thread into my form as follows, im pretty sure th thread file is fine but it refuses to go past here on the main form source, claiming that theres a declaration syntax error and a type name expected??


TRead *ReadThread;

ReadThread = new TRead(false);


is this a correct way to start a new thread??

thanks very much
 
I believe you just need to put #include "mynewthread.h" at the top of the main form.

Chris
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top