Board index » cppbuilder » Thread freezes my app

Thread freezes my app


2003-10-27 07:43:52 PM
cppbuilder34
Hi, I am using TThread for the first time. I set Priority to
tpIdle, but nonetheless my thread seems to get all the CPU time
and my app freezes for that time. This is what I have so far:
An app with Form1 containing Label1, Button1 and Timer1. The
Timer is there to show that my main thread is doing something
(Interval is 10 ms):
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
static count = 0;
Label1->Caption = ++count;
}
The button starts the thread:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
new MyThread(false);
}
Here is my thread:
__fastcall MyThread::MyThread(bool CreateSuspended)
: TThread(CreateSuspended)
{
Priority = tpIdle;
FreeOnTerminate = true;
}
void __fastcall MyThread::Execute()
{
Synchronize(DoSomething);
}
void __fastcall MyThread::DoSomething()
{
Sleep(10000);
}
You see, MyThread does nothing but sleep. So why does my app
freeze? (The counting stops till MyThread is finished).
My system is Win NT, BCB6.
 
 

Re:Thread freezes my app

Thorsten Kettner wrote:
Quote
void __fastcall MyThread::Execute()
{
Synchronize(DoSomething);
}
This tells the main thread to execute DoSomething().
Quote
void __fastcall MyThread::DoSomething()
{
Sleep(10000);
}
The thread executing this function will sleep for 10000 seconds.
Quote
You see, MyThread does nothing but sleep. So why does my app
freeze? (The counting stops till MyThread is finished).
No, your thread tells the main thread to sleep. If you want MyThread to
sleep, when don't use Synchronize().
Ivan Johansen
 

Re:Thread freezes my app

Ivan Johansen < XXXX@XXXXX.COM >wrote:
Quote
>You see, MyThread does nothing but sleep. So why does my app
>freeze? (The counting stops till MyThread is finished).

No, your thread tells the main thread to sleep. If you want
MyThread to sleep, when don't use Synchronize().
Oh, I see. In BCB help it sounded to me like "Use Synchronize,
then you avoid all problems", but reading it again, I understand
now that it is only used in exceptional cases when problems
would occur otherwise. Thank you very much for your very clear
explanation, Ivan.
 

{smallsort}

Re:Thread freezes my app

Quote
Oh, I see. In BCB help it sounded to me like "Use Synchronize,
then you avoid all problems", but reading it again, I understand
now that it is only used in exceptional cases when problems
would occur otherwise. Thank you very much for your very clear
explanation, Ivan
Synchronize makes the Thread use the mainthread for calling the procedure,
it should be used for accessing non-thread safe code, like a VCL component
from more than 1 thread
Seabass