Jump to content
microtronx

Create multiple instances of tApplication with multiple MainThreads?

Recommended Posts

Hi Folks,

 

is it possible to have multiple MainThreads with multiple tApplication instances within one application? Something like this:

 

procedure TForm2.bNewApplicationFormClick(Sender: TObject);
var
	vapplication:tApplication;
	vNewForm:tForm2;
begin
	IsMultiThread:=true;

    vApplication:=tApplication.Create(nil);
	vapplication.Initialize;
	vapplication.MainFormOnTaskbar := True;
	vapplication.CreateForm(TForm2, vnewform);
	vapplication.Run;
end;

I'm searching for a way, having multiple forms with different MainThreads in runtime instead of having multiple application-instances (exe files) started.

Share this post


Link to post
19 minutes ago, Dalija Prasnikar said:

No.

Since we're all crazy programmers ... is "No" acceptable? Any explanation of why No?

Share this post


Link to post

look, each "TApplication" represent a "application = process" on memory!

then, have many "TApplication" means have many process of same process (or be: clones)... but at end, all clones will be the same app!

what you can do is: many forms and changes one or other to be "the main-Form", but not all at same time!

-- because, you cannot have 2 or more "main-Forms" in the same application!

Edited by programmerdelphi2k

Share this post


Link to post

TO BE very SIMPLE, TApplication, with Application instance, is or at least should be a Singleton, because manages the main events loop from Windows.

You can't have more than ONE...
It's the Windows structure of any application.

When you run a program Windows create a process with a main thread which call the delphi main code.
This main code create Application object which manage the messages loop (Windows is a messages-based OS) and on this
messages loop manager is implemented all VCL framework, etc...

Edited by shineworld

Share this post


Link to post

There can be only one main thread. It doesn't need to do anything else but to spawn n number of other threads, each of which can have their own purpose, and can spawn their own sub-threads.

 

Share this post


Link to post

I know about loop, I came form "Clipper summer87"  :classic_cheerleader:

the same "principle" is used in GUI app... a looping "REPEATing" until end message!

Edited by programmerdelphi2k
  • Like 1

Share this post


Link to post
11 minutes ago, microtronx said:

Since we're all crazy programmers ... is "No" acceptable? Any explanation of why No?

It is complicated...

 

In theory you can have Windows application that has multiple GUI threads with each one holding own windows message loop.

 

The answer is no, because VCL is build on the premise of single main (GUI) thread (like many other frameworks as there is really no advantage of having such multithreaded GUI) and you would have to write your own framework to achieve such application and probably would have to rewrite significant parts of the RTL.

  • Like 1

Share this post


Link to post

"A thread is the entity within a process that can be scheduled for execution. All threads of a process share its virtual address space and system resources. Each process is started with a single thread, but can create additional threads from any of its threads"...

https://learn.microsoft.com/en-us/windows/win32/procthread/multiple-threads

 

then, in fact, there is just one "main", the rest is co-adjuvant process

Share this post


Link to post
5 hours ago, microtronx said:

is it possible to have multiple MainThreads with multiple tApplication instances within one application?

No.

 

First, not even considering threads yet, TApplication is simply not designed to run multiple instances at a time.  There are globals that it uses internally which it assumes it has exclusive access to, and will not share well with other instances.  Also, there is plenty of code inside of the VCL that assumes there is only 1 global TApplication object, and won't even consider the existence of other instances.

 

Second, considering threads, the VCL is simply not thread-safe to begin with.  It is not designed to be used in multiple threads at a time.  VCL controls are expected to be used only in the main UI thread which runs the single global TApplication object.  Also, the VCL runs on top of the Win32 API, and API window handles have an affinity to the thread that creates them, which restricts which threads are allowed to access them. And VCL controls simply don't react well when those window handles get messed up when accessed across thread boundaries.

Quote

I'm searching for a way, having multiple forms with different MainThreads in runtime instead of having multiple application-instances (exe files) started.

Don't do it.  That is a very bad design choice.  Handle all of your UI needs in the default main UI thread only.  You can use multiple worker threads to handle your business logic however you want, and you can create a separate TForm to associate with each worker thread if that is what you need.  Just make sure that any data you share across threads is adequately protected from concurrent access, and that you have worker threads synchronize with the main UI thread whenever they need to access the UI.

Edited by Remy Lebeau
  • Like 3

Share this post


Link to post
5 hours ago, microtronx said:

I'm searching for a way, having multiple forms with different MainThreads in runtime instead of having multiple application-instances (exe files) started.

What is it that you think this will achieve? 

Share this post


Link to post
On 12/2/2022 at 2:29 PM, shineworld said:

TO BE very SIMPLE, TApplication, with Application instance, is or at least should be a Singleton, because manages the main events loop from Windows.

You can't have more than ONE...
It's the Windows structure of any application.


This isn't really true. You can have multiple threads that serve multiple windows with different message loops. 

 

Windows supports that. 

 

What you can't do is do that in vcl. 

  • Like 2

Share this post


Link to post
12 minutes ago, shineworld said:

Is Windows UI API SDK (WIN32) thread safe, eg EDIT, COMBOBOX, etc ? 

I missed that... Sorry

Yes. But of course you need to define what thread safe actually means in this context. It's a very general term. For win32 the threading model is that all operations on a window must be performed in the thread that created the window. Queued messages for that window are delivered to the meesage queue of that thread. Nonqueued messages are synchronised onto that thread. 

 

This is not a free for all thread safety where you can do anything you want from any thread. This is why the term thread safety is often not useful. It's more useful to describe and summarise the threading model. In this case I always say that windows have thread affinity. 

  • Like 1

Share this post


Link to post
On 12/2/2022 at 8:07 PM, Dave Nottage said:

What is it that you think this will achieve? 

Main reason is to have multiple forms not "blocking the whole app" i.e. when the cxgrid refreshes data ... This cannot be done in a background thread and we're working with a lot of tables / rows at the same time from different forms.

Share this post


Link to post

maybe be better review your "query" parameters to avoid excessive records on cxGrid, or send it in "batch" to screen! cxGrid can define how much records can be loaded by time!

Share this post


Link to post
1 hour ago, programmerdelphi2k said:

maybe be better review your "query" parameters to avoid excessive records on cxGrid, or send it in "batch" to screen! cxGrid can define how much records can be loaded by time!

Yes cxgrid can be configured, but if you need the grid in GridMode=FALSE to use special functionality (filtering, grouping, sorting, etc) the grid needs to load all data ... and this makes it harder for us.

thanks for you suggestion.

Share this post


Link to post

maybe yes, maybe no!

you can use a specifc "query" to filter your data before bind it to cxGrid!

  • the query-return will so quick than your system!
  • you can works in "offline" mode (cache)

but I think that your better choice would be contact DevExpress for more details!

Edited by programmerdelphi2k

Share this post


Link to post
Quote

Our grid control's DataController is still populated with data in a single thread. However, now some operations (filtering, sorting) can be performed in secondary threads. Please review the "Multi-Threaded Data Processing" help topic for details.

https://supportcenter.devexpress.com/ticket/details/q267890/cxgrid-and-async-fetch

 

Maybe this will help? Googled it in 1 minute

  • Like 1

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

×