Recent

Author Topic: Problem with modal forms and threads  (Read 1693 times)

pleumann

  • Full Member
  • ***
  • Posts: 106
Problem with modal forms and threads
« on: July 11, 2023, 08:22:22 pm »
Hi all,

my main form has several buttons that each open another form in a modal manner. The other forms return their results via ModalResult := xyz, which also closes them. The main form then uses the result of ShowModal do determine what to do next. That all works nicely.

But I have one form that uses a background thread to carry out a lengthy operation. The background thread uses Synchronize on a method of the form to report progress back, so I can update UI elements. That's fine, too. But when the lengthy operation is finished it should also set ModalResult. This basically works, too, but does not take effect immediately. Instead, I need to move the mouse (or generate another event or wait several seconds) for the form to close and the application to proceed. I tried reporting my results to the CurrentThread of the form instead of the main thread, assuming it may be a different one due to the modal magic, but this did not seem to solve it.

What is the proper way to deal with a situation like this?

I'm on an Intel MacOS running Monterey, FPC 3.2.2 and Lazarus 2.2.6.

Cheers!
Joerg

TRon

  • Hero Member
  • *****
  • Posts: 4377
Re: Problem with modal forms and threads
« Reply #1 on: July 12, 2023, 03:30:47 am »
I have honestly no idea (also no code so hard to tell) other than asking to make sure that your thread is actually finished and assuming it did, does firing application.processmessages after setting the modalresult in your modal form perhaps help ?
Today is tomorrow's yesterday.

zeljko

  • Hero Member
  • *****
  • Posts: 1830
    • http://wiki.lazarus.freepascal.org/User:Zeljan
Re: Problem with modal forms and threads
« Reply #2 on: July 12, 2023, 09:56:13 am »
Hi all,

my main form has several buttons that each open another form in a modal manner. The other forms return their results via ModalResult := xyz, which also closes them. The main form then uses the result of ShowModal do determine what to do next. That all works nicely.

But I have one form that uses a background thread to carry out a lengthy operation. The background thread uses Synchronize on a method of the form to report progress back, so I can update UI elements. That's fine, too. But when the lengthy operation is finished it should also set ModalResult. This basically works, too, but does not take effect immediately. Instead, I need to move the mouse (or generate another event or wait several seconds) for the form to close and the application to proceed. I tried reporting my results to the CurrentThread of the form instead of the main thread, assuming it may be a different one due to the modal magic, but this did not seem to solve it.

What is the proper way to deal with a situation like this?

I'm on an Intel MacOS running Monterey, FPC 3.2.2 and Lazarus 2.2.6.

Cheers!
Joerg

It is fixed in 3.0 RC1 and trunk afair.

pleumann

  • Full Member
  • ***
  • Posts: 106
Re: Problem with modal forms and threads
« Reply #3 on: July 12, 2023, 04:20:02 pm »
It is fixed in 3.0 RC1 and trunk afair.

Oh, that sounds great! I expected to have run into either a weird edge case or my own lack of up-to-date knowledge about Delphi/Lazarus programming (as stated elsewhere, I've used Pascal a lot, but that was 25 years ago).

Thanks a lot! I'll try to install 3.0 in parallel on one of my machines.

Best regards
Joerg

pleumann

  • Full Member
  • ***
  • Posts: 106
Re: Problem with modal forms and threads
« Reply #4 on: August 13, 2023, 12:50:15 pm »
It is fixed in 3.0 RC1 and trunk afair.

Since I just ran into it again (and still do not want to upgrade to 3.0 RC1 for production), would you mind pointing me to the commit that fixes it? I could cherry-pick that into my Lazarus 2.2.6 LCL or try to come up with a workaround until 3.0 is released. (I did grep the git log, but couldn't find the commit.)

Best regards
Joerg

pleumann

  • Full Member
  • ***
  • Posts: 106
Re: Problem with modal forms and threads
« Reply #5 on: August 13, 2023, 02:05:52 pm »
Nevermind, I may have found it via the bug tracker. Testing now...

pleumann

  • Full Member
  • ***
  • Posts: 106
Re: Problem with modal forms and threads
« Reply #6 on: August 13, 2023, 02:15:36 pm »
Cherry-picking 34ab4cb2 fixed my problem.

 

TinyPortal © 2005-2018