Hello Michael,

> Thanks for all the replies. I mistakenly convinced myself that an object
> instance belonged to a particular thread as it would if it was in
> another process (space). However I realise this is not the case.

Code is always executed in the thread context of the caller. For example
a thread's constructor or destructor is *not* executed in the thread
context of this thread, but in the thread context of the one who calls
this constructor.

Only code executed from the Execute method of that thread is executed in
this thread context. Also if you create a hidden window in the execute
method, any messages processes by that window (or better say by the
message pump you write for that window) will also be executed in that
same thread context.

You can always call GetCurrentThreadID to check in wich thread you are
running.

> I am confused as to why you would pass messages to communicate between
> threads though, could you not just update a central memory location? Or
> is this because of the race condition? I think I just need to find a
> real world example to put it into context.

A memory location shared by threads is possible, but normally not the
good programming technique. You can protect it by a TCriticalSection, or
you can use various synchronization techniques. But if you have need a
lot of synchronization then you in fact disable multithreading because
the threads have to wait for others.

You can create a hidden window (in Execute method, and also Destroy it
in the same Execute method). Then you can post messages to that window
just as you can post messages to any window, eg a TForm. You only have 2
parameters (See PostMessage in winapi help). If you need string data
(PChar) or records then you can assign memory for it, put the pointer in
the WParam sne post it. You can free the memory in your thread when you
receive the data.

Another way is to use PostThreadMessage (see winapi help). For both you
have to create a message pump, but for the latter it is not needed to
create a hidden window. Windows automatically create a mesage queue if
you start pumping in a thread. This is good explained in the winapi help
also.

---
Rgds, Wilfried
http://www.mestdagh.biz

_______________________________________________
Delphi mailing list -> [email protected]
http://www.elists.org/mailman/listinfo/delphi

Reply via email to