TThread descendant with public events

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

TThread descendant with public events

Graeme Geldenhuys-2
Hi,

What is the rules regarding a TThread descendants and public events. I
created a thread that has OnXXX events. These events are triggered in the
Execute method of that event.

Now my main event (which manages the application GUI) hooked into these
event by implementing event handlers. Those event handlers take the data
from the event and updates a StringGrid.

Is this safe to do?

Must the thread execute the event via a Synchronize() call, or is it now
needed? The reason I'm not sure here, is because the thread doesn't actuall
update the GUI component (stringgrid in this example) directly, it just
triggers the event.


Regards,
  - Graeme -

--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://opensoft.homeip.net:8080/fpgui/

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: TThread descendant with public events

Sven Barth-2
On 01.11.2010 09:57, Graeme Geldenhuys wrote:

> Hi,
>
> What is the rules regarding a TThread descendants and public events. I
> created a thread that has OnXXX events. These events are triggered in the
> Execute method of that event.
>
> Now my main event (which manages the application GUI) hooked into these
> event by implementing event handlers. Those event handlers take the data
> from the event and updates a StringGrid.
>
> Is this safe to do?
>
> Must the thread execute the event via a Synchronize() call, or is it now
> needed? The reason I'm not sure here, is because the thread doesn't actuall
> update the GUI component (stringgrid in this example) directly, it just
> triggers the event.

The code is still run in the thread's context, no matter whether the
code is in your TThread class or e.g. a global procedure. So you should
use Synchronize to trigger the event.

Regards,
Sven
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: TThread descendant with public events

Martin Schreiber-2
In reply to this post by Graeme Geldenhuys-2
On Monday, 1. November 2010 09.57:21 Graeme Geldenhuys wrote:

> Is this safe to do?
>
No. An onxx event is a tmethod. "Triggering" the event means calling the
method -> it runs in context of the calling thread.

Martin
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: TThread descendant with public events

Graeme Geldenhuys-2
In reply to this post by Sven Barth-2
Op 2010-11-01 11:06, Sven Barth het geskryf:
>
> The code is still run in the thread's context, no matter whether the
> code is in your TThread class or e.g. a global procedure. So you should
> use Synchronize to trigger the event.


Thank you very much Sven and Martin. What you said then confirms why my
application crashes after X amount of lines were added to the StringGrid,
but worked correctly when the event was triggered inside the Synchronize()
call.



Regards,
  - Graeme -

--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://opensoft.homeip.net:8080/fpgui/

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal