( Delphi fmx ) Can you create UI controls in a background thread without blocking user interface

  

I’m wondering if it’s possible to create UI elements , even a complete form, in a background thread ( “without using it of course” ) , and once it’s done, make it available in main thread to be shown.

It’s relatively easy now to separate time consuming data operations in background threads and synchronize the results with your main thread but what if creating the UI itself IS the time consuming operation ?

If possible, you could maybe have a fast startup screen and then start a background thread to create a set of forms. Whenever one is ready, it enables a menu item in the main thread so that it can be used.

I tried simple code, but it freezes immediatly. Is it possible ?

Execute in main program :


// declare var in main form
public
{ Public declarations }
lForm : TForm ;


// Execute e.g. with button click in main form
TThread.CreateAnonymousThread( procedure begin
// this stops application from running when the form is show
// in the synchronize part
lForm := TForm1.Create(Self);

TThread.Synchronize(nil,
procedure begin
// This does not stops the application but freezes the gui of course
//lForm := TForm1.Create(Self);
lForm.Parent := Self ;
lForm.Show ;
end );

end ).Start ;`

procedure TForm1.FormCreate(Sender: TObject);
begin
sleep(2000);
end;

If it’s not possible, how could you do this in the main thread while still ‘simulating’ that your main thread is responsive ? ( by calling something like application.processmessages regularly or so ? )

I’m using Delphi Rio 10.3, fmx framework.

Comments are closed.