Recent

Author Topic: How to create form in thread?  (Read 2466 times)

Warfley

  • Hero Member
  • *****
  • Posts: 1428
Re: How to create form in thread?
« Reply #15 on: May 22, 2023, 02:12:02 pm »
The thing is for most things where threads are currently used they are not actually needed. The thing is currently most Lazarus/Pascal libraries have no real alternative. E.g. fpWeb, as I said there is absolutely no reason to use threading for web fetches, yet afaik TFPHTTPClient does not allow for non blocking fetches. So you often see people use threads for things where they are absolutely not needed in principle, but in practice there is just no (easy to use) alternative

lazpas

  • Jr. Member
  • **
  • Posts: 70
Re: How to create form in thread?
« Reply #16 on: June 02, 2023, 03:28:54 am »
Thank you friends for discussing this issue and giving advice.

I'm writing an unpacker with the decompression function in a thread that can pause, like 7z, when the file already exists, and needs to open a window for the user to choose whether to ignore or replace.

I solved this problem with Synchronize.

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1235
    • Lebeau Software
Re: How to create form in thread?
« Reply #17 on: June 02, 2023, 06:37:23 pm »
Basically the windows API does not support threaded access to GUIs

That is not true.  On Windows, any thread can have its own UI and its own message loop, Microsoft puts no restriction on that at all.  And any thread can send messages to a window in another thread.

The VCL's restriction is simply a matter of implementation, as the library is not thread-safe in general, and accessing/manipulating UI objects across thread boundaries carries some risk.  So by convention (and common sense), UI access should be restricted to a single thread only.

I imagine the LCL is the same way for similar reasons.
« Last Edit: June 02, 2023, 06:39:02 pm by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

Warfley

  • Hero Member
  • *****
  • Posts: 1428
Re: How to create form in thread?
« Reply #18 on: June 03, 2023, 12:35:49 am »
That is not true.  On Windows, any thread can have its own UI and its own message loop, Microsoft puts no restriction on that at all.  And any thread can send messages to a window in another thread.
But even with that you are extremely limited in what is possible, as when you want to for example create any control (like a Window, Button, etc.) this control is restricted to the message queue of the thread that called CreateWindow. Meaning sure you can create a button in another thread, when that thread has a message queue, but as soon as that thread stops serving that queue, that control seizes to function. So you can't create (or destroy for that matter) controls of other threads.
So you aren't modifying the GUI of the main thread, as much as building a parallel GUI (which may just consist of a few controls living on other controls from another thread or even process) in your worker thread that lives and dies with that thread.

And this can be a major problem for the VCL or LCL, because not all LCL controls are even a single winapi window class, but may consist of multiple windows that are dynamically created (e.g. TDirectoryEdit consists of an Edit and a Button). So while updating of certain properties of a winapi element may be possible with a PostThreadMessage or SendMessage call, if a higher level control requires to create or destroy any subcontrols for an operation it can't be made cross platform on the Windows API.

So I still stick to my point, the WinAPI GUI api is not build for cross platform applications. So while some things may be possible, it is not intended to be used accross threads, and things like creating or destroying controls/windows simply does not work because it was never intended to be used that way.
« Last Edit: June 03, 2023, 12:38:55 am by Warfley »

Mr.Madguy

  • Hero Member
  • *****
  • Posts: 822
Re: How to create form in thread?
« Reply #19 on: June 03, 2023, 10:06:49 am »
Well, Delphi supports MDI applications, where, as I know, each MDI frame runs in it's own thread. And it's enough, because at the end what we want - is to achieve "application inside application" goal. Dedicating thread to each control - is too much to ask. You don't need thread for control, that can run long task in it's event handler - you can always put this specific task to separate thread and keep all controls within main thread. Just don't misuse tools, you're given. Use them properly. You're making your life harder instead of making it easier.
« Last Edit: June 03, 2023, 10:12:24 am by Mr.Madguy »
29.12.2021 - migration to DynamicData 4.1 is completed - complete overhaul of data access driver.
My project still requires full Delphi 2009 support to be ported to Lazarus.
It's time to finally do it, because Delphi 2009 is 14 years old.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5200
  • Compiler Developer
Re: How to create form in thread?
« Reply #20 on: June 03, 2023, 07:22:32 pm »
Well, Delphi supports MDI applications, where, as I know, each MDI frame runs in it's own thread. And it's enough, because at the end what we want - is to achieve "application inside application" goal.

Wrong. In Delphi MDI applications use the same GUI thread. It's not “application inside application”, it's “multiple forms inside the same window”.

 

TinyPortal © 2005-2018