Recent

Author Topic: Showing dynamically created form (Lazarus/Linux)  (Read 4358 times)

odvesims

  • Full Member
  • ***
  • Posts: 176
Showing dynamically created form (Lazarus/Linux)
« on: August 18, 2015, 03:50:01 pm »
Hey There!

Im my application I send request to a server via socket. While the whole socket request/receiving process is taking place I show a dynamically created form that essentially blocks the UI and shows a message telling the user which action is taking place. It closes after the socket's received an answer or after it's timed out.

This works perfectly under Windows, however i cant get it to work on Unix systems (Ubuntu version 10). The UI simply blocks but the form is not shown. It shows if I, for instance, put a ShowMessage in between the form "Show" method and the "Close" method.

Anyone knows why is this happening and what'd be the way to fix it.. Anyone thinking of a different approach (sure there'd be a "cleaner" way to do it, perhaps using threads? No clue there though...).

Thanks in advance,

Oscar

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Showing dynamically created form (Lazarus/Linux)
« Reply #1 on: August 18, 2015, 05:04:37 pm »
Show some code.

odvesims

  • Full Member
  • ***
  • Posts: 176
Re: Showing dynamically created form (Lazarus/Linux)
« Reply #2 on: August 18, 2015, 05:09:09 pm »
Hey Engkin

It's simple, i create the Form using the following

DialogForm:= TForm.Create(Application);
DialogForm.Position:= TPosition.poMainFormCenter;
DialogForm.SetBounds(100, 100, 254, 116);
DialogForm.Caption:= 'Please, wait...';
DialogForm.AlphaBlendValue := 230; // 0..255
DialogForm.AlphaBlend := True;
DialogForm.BorderStyle:= TFormBorderStyle.bsDialog;

Then i use the DialogForm.Show command to make the TForm visible. But it's not working under UNIX systems.

eny

  • Hero Member
  • *****
  • Posts: 1646
Re: Showing dynamically created form (Lazarus/Linux)
« Reply #3 on: August 18, 2015, 05:15:00 pm »
Maybe adding 'Application.ProcessMessages' right after the Show helps.
All posts based on: Win10 (Win64); Lazarus 3_4  (x64) 25-05-2024 (unless specified otherwise...)

garlar27

  • Hero Member
  • *****
  • Posts: 652
Re: Showing dynamically created form (Lazarus/Linux)
« Reply #4 on: August 18, 2015, 05:18:21 pm »
AFAIK the form would be blocked on Windows too.

To fix that put a TTimer on your form and set the Interval to 500 ms or 1000 ms, and set Enabled to true. In the OnTimer event put this code (in case you use form disigner):
Code: [Select]
Application.ProcessMessages
Add this to your code:
1- Add this method to your class
Code: [Select]
procedure OnTimerEvent(Sender: TObject);

procedure TMyClass.OnTimerEvent(Sender: TObject);
begin
  Application.ProcessMessages
end;
2- Add the followin code to your function:
Code: [Select]
var
  ....
  MyTimer: TTimer;
begin
  DialogForm:= TForm.Create(Application);
  DialogForm.Position:= TPosition.poMainFormCenter;
  DialogForm.SetBounds(100, 100, 254, 116);
  DialogForm.Caption:= 'Please, wait...';
  DialogForm.AlphaBlendValue := 230; // 0..255
  DialogForm.AlphaBlend := True;
  DialogForm.BorderStyle:= TFormBorderStyle.bsDialog;

  MyTimer := TTimer.Create(DialogForm);
  MyTimer.Interval := 500;
  MyTimer.OnTimer := @OnTimerEvent;
  MyTimer.Enabled := true;

  //
end;

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Showing dynamically created form (Lazarus/Linux)
« Reply #5 on: August 18, 2015, 05:52:21 pm »
From TApplication.CreateForm source code:
Code: [Select]
...
    if AForm.FormStyle = fsSplash then
    begin
      // show the splash form and handle the paint message
      AForm.Show;
      AForm.Invalidate;
      ProcessMessages;
    end;
...

I suggest you do that same:
Code: [Select]
...
  DialogForm.Show
  DialogForm.Invalidate;
  Application.ProcessMessages;  //<--- edit: correction
...

Similar to solutions presented by eny and garlar27.

Another possibility is to use QueueAsyncCall for the socket process you have after DialogForm.Show.
« Last Edit: August 18, 2015, 06:24:37 pm by engkin »

odvesims

  • Full Member
  • ***
  • Posts: 176
Re: Showing dynamically created form (Lazarus/Linux)
« Reply #6 on: August 18, 2015, 07:36:15 pm »
Maybe adding 'Application.ProcessMessages' right after the Show helps.

This did the trick!! Thank you so much!!! ^_^

 

TinyPortal © 2005-2018