multithreading - ( Delphi fmx ) 能否在后台线程中创建 UI 控件而不阻塞用户界面

标签 multithreading user-interface delphi firemonkey

我想知道是否可以在后台线程中创建 UI 元素,甚至是完整的表单(“当然不使用它”),并且一旦完成,就可以在主线程中显示它。

现在在后台线程中分离耗时的数据操作并将结果与​​主线程同步是相对容易的,但是如果创建 UI 本身就是耗时的操作怎么办?

如果可能的话,您可能会有一个快速启动屏幕,然后启动一个后台线程来创建一组表单。每当准备就绪时,它就会在主线程中启用一个菜单项,以便可以使用它。

我尝试了简单的代码,但它立即卡住。这可能吗?

在主程序中执行:

    ...
    // 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;
    ...



如果不可能,那么如何在主线程中执行此操作,同时仍然“模拟”主线程具有响应能力? (通过定期调用诸如 application.processmessages 之类的东西?)

我使用的是 Delphi Rio 10.3,fmx 框架。

最佳答案

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.

没有。

从后台线程创建和访问 UI 控件不是线程安全的。

虽然您可以创建一些看似有效的概念验证代码,但此类代码本质上是有问题的。它可能会随机失败。

关于multithreading - ( Delphi fmx ) 能否在后台线程中创建 UI 控件而不阻塞用户界面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58553535/

相关文章:

不同 DLL 甚至线程之间的 C++ 安全字符串

wpf - WPF 的 IsDirty 实现

php - 对于 Web 应用程序,将多个页面上的嵌套讨论分开的最佳方法是什么?

delphi - 如何管理delphi单元中的循环引用?

c# - 进程因 StackOverflowException 而终止

java - 用同步块(synchronized block)替换 volatile 不起作用

multithreading - 误解单线程和多线程编程之间的区别

c++ - 具有原生外观和感觉的轻量级C/C++ GUI库

Delphi:确定泛型的实际类型?

mysql - 该系统最有效的架构是什么? (推或拉)