Forum > General

How to create form in thread?

<< < (2/5) > >>

lazpas:
eljo,Zoran,Warfley,cai,thanks for the reply.

@Zoran,Very nice and very inspiring.Thanks.
@Warfley,@cai,Good to know.Thanks.

Mr.Madguy:
Not sure about other platforms, but it's actually possible on Windows. You just should remember, that separate thread should have it's own message loop. Works for me in Delphi, except terminating. If Form2 is closed fist, application terminates properly, but thread doesn't exit, if Form1 is closed first and Form2.Close is called by it. Problem is most likely caused by posting PostQuitMessage to wrong message queue. No time to solve it now. Form1 becomes slow in Lazarus.

Peter H:
It is possible in Windows to have a thread with an own window and message loop.
This is described in the manual for wxWidgets, but it is disrecommended.
(Most GUI frameworks are strictly singlethreaded. I believe the reason is: The screen hardware is not reentrant and somewhere the access must be serialized anyway. So multithreaded access has no performance advantage)

It should be possible in Pascal to have a local procedure in a thread, which can be called by the main (GUI) thread, but can access local variables of the thread, and I think this fulfills the same purpose.

The following code works in fpc trunk, however it is experimental and I do not know if this is correct code.
{$modeswitch functionreferences} is required.


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---type pref=reference to procedure; procedure TMyThread.Execute;var  newStatus: string;   procedure test;  begin    Form1.Caption := newstatus; //fStatusText;  end; begin  newstatus := 'TMyThread Starting ...';  Synchronize(pref(@test));  ..... 

Mr.Madguy:
My idea about why it would be needed - to keep UI as responsive as possible, while avoiding lots of Application.ProcessMessages calls. Because at the end it's weak point in event-based UI design. It looks unnatural. You should know, that such design is inherited from Win3.x era, when there wasn't hardware multitasking. First it was carried to Win9x/NT to keep code as compatible with Win3.x, as possible. It was also good idea, because such design was automatically minimizing CPU load. Because when application's main thread is always blocked, while it has nothing to do - then it doesn't consume any CPU cycles. But in modern situation, I guess, it's always better to use threads for long tasks. What we talk here about - is situation, when we need two or more windows, that can perform some blocking operations, but shouldn't block each other. Yeah, such situation is more theoretical, than practical, because there are always other ways to achieve this goal.

eljo:

--- Quote from: Mr.Madguy on May 21, 2023, 03:20:52 pm ---My idea about why it would be needed - to keep UI as responsive as possible, while avoiding lots of Application.ProcessMessages calls. Because at the end it's weak point in event-based UI design. It looks unnatural. You should know, that such design is inherited from Win3.x era, when there wasn't hardware multitasking. First it was carried to Win9x/NT to keep code as compatible with Win3.x, as possible. It was also good idea, because such design was automatically minimizing CPU load. Because when application's main thread is always blocked, while it has nothing to do - then it doesn't consume any CPU cycles. But in modern situation, I guess, it's always better to use threads for long tasks. What we talk here about - is situation, when we need two or more windows, that can perform some blocking operations, but shouldn't block each other. Yeah, such situation is more theoretical, than practical, because there are always other ways to achieve this goal.

--- End quote ---
Here is an example that benefits from a threaded forms design, financial application with real time stock graphs (candle sticks etc) it would be nice to be able to allow multiple windows with out one tripping on the other's update times.

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version