Board index » delphi » THREAD QUESTION!!

THREAD QUESTION!!

How do I create a COMPONENT declared globally from a threads execute
method?
.
e.g.--

Type
MyThread = class(TThread)
  Protected
   Procedure Execute;Override;

 TESDMain = class(TForm)
    MyButton : TButton;
    procedure MyButtonClick(Sender: TObject);

Var
    MyComponent : Array[1..10] of TMyObject;

Begin
    Procedure MyThread.Execute;
    Begin
        //Do some stuff.......
{1}     MyComponent[1] := TMyObject.Create(self); <---- This
    End;

    Procedure TForm1.MyButtonClick(Sender:TObject);
    Begin
        T_Thread := MyThread.Create(False);
{2}
    End;
End.

Don't worry if I made a spelling mistake somewhere in this example.
I just typed this in to show what I mean.
It gives me the message 'Incompatible Types:'TComponent' and
'MyThread'.
If I put statement  {1} at {2} it works of course.

Thank for any help.

That`s very funny Scotty. Now beam down my clothes!

Remove dashes in E-Mail.

 

Re:THREAD QUESTION!!


I'm pretty sure the problem is the "self" in "MyComponent[1] :=
TMyObject.Create(self); <---- This".  In that location, self refers to the
TThread, which isn't a valid parent for what I'm assuming is a visual
component. Try:

MyComponent[1] := TMyObject.Create(Form1);

or, if you are going to take care of freeing the component somewhere in your
thread, use:

MyComponent[1] := TMyObject.Create(nil);

  -Chris

Quote
Anton wrote in message <35b70f05.24711...@196.4.160.8>...
>How do I create a COMPONENT declared globally from a threads execute
>method?
>.
>e.g.--

>Type
>MyThread = class(TThread)
>  Protected
>   Procedure Execute;Override;

> TESDMain = class(TForm)
>    MyButton : TButton;
>    procedure MyButtonClick(Sender: TObject);

>Var
>    MyComponent : Array[1..10] of TMyObject;

>Begin
>    Procedure MyThread.Execute;
>    Begin
> //Do some stuff.......
>{1} MyComponent[1] := TMyObject.Create(self); <---- This
>    End;

>    Procedure TForm1.MyButtonClick(Sender:TObject);
>    Begin
> T_Thread := MyThread.Create(False);
>{2}
>    End;
>End.

>Don't worry if I made a spelling mistake somewhere in this example.
>I just typed this in to show what I mean.
>It gives me the message 'Incompatible Types:'TComponent' and
>'MyThread'.
>If I put statement  {1} at {2} it works of course.

>Thank for any help.

>That`s very funny Scotty. Now beam down my clothes!

>Remove dashes in E-Mail.

Other Threads