We help IT Professionals succeed at work.

Basic question regarding Threads

PeteMulford
PeteMulford asked
on
189 Views
Last Modified: 2010-04-05
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
Comment
Watch Question

Russell LibbySoftware Engineer, Advisory
CERTIFIED EXPERT
Top Expert 2005

Commented:
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


Author

Commented:
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
Software Engineer, Advisory
CERTIFIED EXPERT
Top Expert 2005
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.