Jump to content
alogrep

Show a Form from inside a Thread

Recommended Posts

HI.

I hope someone can give me a hint  here. 

Printque is a Form created in the Main Thread, just before calling this thread.

Its  Formcreate contains this line:

     Tstuckthread.Create(Tprintqueue(self),printernames);

The problem is in the showform procedure at the line .A.show

It gives no error, but the app becomes unresponsive. The main form is hidden, and I have to stop the app.I

Tstuckthread= class(TThread)
private
FForm: TFORM;
L:tSTRINGLIST;
function  STUCK(title:string;l:Tstringlist): boolean;
procedure listjobs(allprinters: string);

protected

public
constructor Create(ownerForm:TFORM;printernames:string; ARUNS: INTEGER=0);
destructor Destroy; override;
procedure Execute(); override;
procedure showform;
published
end;


constructor Tstuckthread.Create(ownerForm: TFORM;printernames: string;ARUNS: INTEGER=0);
begin
L:=tSTRINGLIST.CREATE;
FForm:= ownerForm;
Self.FreeOnTerminate := True;
inherited Create;
end;

destructor Tstuckthread.Destroy;
begin
inherited;
L.FREE;
end;

procedure Tstuckthread.Execute;

begin
   done :=FALSE;
 while not Terminated AND NOT  done do  begin
   <get printjobs list and save it in L......>

  if  L.count>0 then BEGIN   /// fine HERE
    Tprintqueue(FForm).MEMO1.LINES.ASSIGN(L);  // fine HERE
    Synchronize(SHOWForm);
    SLEEP(500);
  END;
  DONE:=TRUE;
 end;
end;

 And here is the showform() procedure;

procedure Tstuckthread.showform;
VAR
  A: Tprintqueue;
begin
  A:=Tprintqueue(FForm);
TRY
IF NOT A.VISIBLE THEN
  A.show;  // PROBLEM HERE.
EXCEPT
  a.free;// 4 testing. It never gets there.
END;

 

Share this post


Link to post

Sorry.

This line was wrong.

  Printque is a Form created in the Main Thread, just before calling this thread.

shold have been 

  Printque is a Form created in the Main Thread, 

Share this post


Link to post

The only problem I see with this code is that you are not Synchronize'ing access to the Form's Memo. You must do so. Move that Assign() call into your ShowForm() method.

 

//Tprintqueue(FForm).MEMO1.LINES.ASSIGN(L);
Synchronize(ShowForm);
...
procedure Tstuckthread.ShowForm;
begin
  Tprintqueue(FForm).Memo1.Lines.Assign(L);
  FForm.Show;
end;

 

Edited by Remy Lebeau

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×