Link to home
Start Free TrialLog in
Avatar of PeteMulford
PeteMulford

asked on

Basic question regarding Threads

I'm new to using threads and have a couple of simple (i think!) questions!

1) I'm following an example that uses that posts message back to the form when valid data has been assigned a shared memory resource. In the main form unit it has:
const
  WM_DATA_IN_BUF = WM_APP + 1000;
    procedure HandleNewData(var Message: TMessage); message WM_DATA_IN_BUF;

and the thread then has:
      PostMessage(PrimeFrm.Handle, WM_DATA_IN_BUF, 0, 0);

Am I correct in my understanding that the value '1000' used can be any value as long as i havent used it before?

2) The example also has:
type
  TPrimeFrm = class(TForm)
  private
    FStringBuf: TStringList;
  public
    property StringBuf: TStringList read FStringBuf write FStringBuf;
  end;

I'm not to sure that I understand the relationship between FstringBuf and stringBuf? Is this basically saying that there is a private property FstringBuf that the to the 'world outside' the form can be read and written to using StringBuf? If thats the case what is the advantage? Why 'F' StringBuf.


3) I have a thread that requires several tbitmap and Tmemorystream variables. I was going to declare and initialise these in the thread's .execute event, but I was wondering whether there was an oncreate event and whether I could initialise them there? I'm new to multithreaded programming so I thought I would ask before trying incase there are any problems that would not be apparent.

Thanks
Pete
Avatar of Russell Libby
Russell Libby
Flag of United States of America image

1.) It can be any value between $8000 (32768) and $BFFF (49151) when using the WM_APP message contant. 1000 is fine as used above
2.) FStringBuf is the property *storage*, and StringBuf is the property that external (to the object) code uses to access the property storage.
3.) You can set (create) these in the constructor of the thread (by overriding the Create), but read this carefully:

When creating / setting internal properties in a TThread constructor, ensure that you do this work before calling the inherited Create.

eg:

 TSomeThread =  class(TThread)
  private
     // Private declarations
     FStream:   TMemoryStream;
  protected
     // Protected declarations
     procedure      Execute; override;
  public
     // Public declarations
     constructor    Create;
  end;

constructor TSomeThread;
begin

  // Set starting parameters
  FStream:=TMemoryStream.Create;

  // Perform inherited
  inherited Create(False);

end;

If you reversed the order and performed the inherited Create first, then created the stream, there is a chance (if the thread is not suspended) that the Execute code will be entered before the constructor has finished. The reason is that the Create occurs in the calling thread (normally the main thread) and the Execute takes place in the newly created (and running) thread. Just something to be aware of...

Regards,
Russell


Avatar of PeteMulford
PeteMulford

ASKER

Hi

Thats cool, can I just clarify some issues with qu2?

I found that  FStringBuf has the F as its considered a 'field' of a class, and that you refer to  Stringbuf as 'propertiy'. Years ago I learnt oop using smalltalk which as I remember had classes and attributes. The attributes would have get and set accessor methods to access  the stored data. Am I correct in believing that I could replace the StringBuf with getStringBuf and setStringBuf methods? Are there advantages/disadvantes in doing so? Are the words 'field', 'property' and attribute basically the same thing?

Thanks
Pete
ASKER CERTIFIED SOLUTION
Avatar of Russell Libby
Russell Libby
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial