Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Component executing thread in design time 1

Status
Not open for further replies.

djjd47130

Programmer
Nov 1, 2010
480
US
I'm building a component (as mentioned in some previous posts) which has a custom thread of mine inside. I have come to find out that when I drop this final component onto a form (or data module), it executes the thread somehow, when I don't have a single thing in this component which shall execute the thread just on create. I made it so it only executes on demand. I also made sure that in the thread's creation, I'm forcing it to create suspended. The only place I ever execute this thread is in one procedure which resumes the thread, and is only called on demand (like a button click) but I have proof that it is in fact executing anyway.

The question is, besides figuring out where it's executing, how do I use conditional defines to block it from performing certain code in design time?

something like...

Code:
procedure RefreshList;
begin
  if assigned(fOnStart) then fOnStart(Self);
  {$IFNDEF DESIGN}
  Self.Resume;
  {$ENDIF}
end;

I know this is possible, but what's the conditional to use?

Also, any suggestions on the thread executing without me telling it to?

This is the inherited create constructor for the thread...
Code:
constructor TNetResourceScanner.Create;
begin
  fBusy:= True; //Tell anything trying to access it that it's busy right now
  try
    inherited Create(True); //Creating suspended
    FreeOnTerminate:= True; //Make sure it free's when it terminates
    Priority:= tpHigher; //Speed things up a little
    fStop:= True; //If for some reason it is executing, this tells the thread to stop executing
  finally
    fBusy:= False; //Not busy anymore
  end;
end;


JD Solutions
 
if (csDesigning in ComponentState) is what you want in the overridden descendant of TComponent.Loaded. For example, the code below shows a message upon loading the project, but should be silent on run-time.

Code:
procedure TSysTrayIcon.Loaded;
  { initializations of the control }
  const
    warning_message = 'Balloon tips are not configured for this system '
       + 'and will not work in the TSysTrayIcon component.' + #13#10
       + 'Please contact your administrator. For details see Microsoft KB #307729';
  begin
    inherited loaded;
    // enforce defaults
    if (csDesigning in ComponentState) then
      begin
        if BalloonTipsAllowed = false then
          ShowMessage(warning_message);
      end
    else
      begin
        if FIcon.Empty then FIcon.Assign(Application.Icon);
        if TipInfo = '' then
          TipInfo := 'You need to assign your own balloon tip message here.';
      end;
  end;

It is not possible for anyone to acknowledge truth when their salary depends on them not doing it.
 
Components always execute in design time or run time. Like with a TMemo, it has all its normal run-time stuff, but it still needs to execute in design time to draw the control on the design form, handle things like property editors and so on. The method above generally seems to be what distinguishes function within the two different places.

It is not possible for anyone to acknowledge truth when their salary depends on them not doing it.
 
Thank you. Now I just need to figure out why this custom thread of mine is executing when I'm not making it execute.

JD Solutions
 
I figured it out a while back actually. It was very stupid too. It wasn't that the thread was executing on its own - it was because I forgot I had another auto creating data module in the background which had this component dropped into it, which it was made to automatically execute on create... Cheers for dumb moments!


JD Solutions
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top