Recent

Author Topic: [SOLVED] How to Show a form before app terminate?  (Read 773 times)

incendio

  • Sr. Member
  • ****
  • Posts: 291
[SOLVED] How to Show a form before app terminate?
« on: September 20, 2023, 05:38:14 am »
Hi guys,

I have an app that only have one form which is main form.

On this form there is a progress bar.

The app idea are :
1. Run a database quary
2. Process result from the query
3. Step the progress bar for operation no 1 & 2.
4. Terminate app when all process done.

Codes are something like these ;
Code: Pascal  [Select][+][-]
  1. procedure TMainFrm.FormShow(Sender: TObject);
  2. begin
  3.    P.Position:= 0; // P is a progress bar
  4.    OpenDb();
  5.    
  6.    Q.Open(); // Q is TIBQuery
  7.    Q.Last;
  8.    Q.First;
  9.  
  10.    P.Max := Q.RecordCount;
  11.  
  12.    while not(Q.EOF) do
  13.     begin
  14.       //do some process
  15.       P.StepIt;
  16.       application.ProcessMessages;
  17.       Q.Next
  18.     end;
  19.     application.Terminate;
  20. end;
  21.  

The app runs OK, but couldn't see progress bar because the form wasn't show.

« Last Edit: September 20, 2023, 06:26:02 am by incendio »

Fibonacci

  • Hero Member
  • *****
  • Posts: 647
  • Internal Error Hunter
Re: How to Show a form before app terminate?
« Reply #1 on: September 20, 2023, 06:07:15 am »
Your code in FormShow is executed just before the form is shown.

1. Put TTimer on form
2. Set Enabled to True
3. Set Interval to 1 (ms)
4. Put your code inside OnTimer event
5. At first line of code in OnTimer set Timer Enabled to False

incendio

  • Sr. Member
  • ****
  • Posts: 291
Re: How to Show a form before app terminate?
« Reply #2 on: September 20, 2023, 06:25:47 am »
Your code in FormShow is executed just before the form is shown.

1. Put TTimer on form
2. Set Enabled to True
3. Set Interval to 1 (ms)
4. Put your code inside OnTimer event
5. At first line of code in OnTimer set Timer Enabled to False

That solved the problem.

Thanks for your help, appreciated.

TRon

  • Hero Member
  • *****
  • Posts: 3789
Re: [SOLVED] How to Show a form before app terminate?
« Reply #3 on: September 20, 2023, 06:52:32 am »
You can also do without using a timer by adding a handler to the OnActivate event of your form:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormActivate(Sender: TObject);
  2. begin
  3.   // remove Activate event handler so that is only invoked once. Do it at the first opportunity.
  4.   Self.OnActivate := nil;
  5.  
  6.   PerformLengthyOperation;
  7.  
  8.   // depending if you want to inform the user or not you can display a message here
  9.   // to tell how the progress went (interrupted by the user for example).
  10.  
  11.   // Closing the last form of an application will automatically terminate that application
  12.   Self.Close;
  13. end;
  14.  
I do not have to remember anything anymore thanks to total-recall.

incendio

  • Sr. Member
  • ****
  • Posts: 291
Re: [SOLVED] How to Show a form before app terminate?
« Reply #4 on: September 20, 2023, 07:24:31 am »
You can also do without using a timer by adding a handler to the OnActivate event of your form:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormActivate(Sender: TObject);
  2. begin
  3.   // remove Activate event handler so that is only invoked once. Do it at the first opportunity.
  4.   Self.OnActivate := nil;
  5.  
  6.   PerformLengthyOperation;
  7.  
  8.   // depending if you want to inform the user or not you can display a message here
  9.   // to tell how the progress went (interrupted by the user for example).
  10.  
  11.   // Closing the last form of an application will automatically terminate that application
  12.   Self.Close;
  13. end;
  14.  
This is good stuff, thanks.

Prefer this method now.

TRon

  • Hero Member
  • *****
  • Posts: 3789
Re: [SOLVED] How to Show a form before app terminate?
« Reply #5 on: September 20, 2023, 07:56:08 am »
@incendio:
Glad to hear that the provided solution was able to help you out. You're welcome.

The example can be extended to fulfill other needs in case you might need it (in the future, non related to your current question):
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormActivate(Sender: TObject);
  2. const
  3.   // typed contant will keep its value for every invocation of this handler
  4.   // similar to as a global variable does.
  5.   FirstTime : boolean = true;
  6. begin
  7.   // In case you need to use the form activate event for other stuff as well
  8.   // then set the boolean value to false at the first invocation of the
  9.   // activate event
  10.   if FirstTime then
  11.   begin
  12.     FirstTime := false;
  13.     PerformLengthyInitializationOperationsOnlyOnce;
  14.  
  15.     // if you do not wish for the other onactivate code to be invoked
  16.     // then you can call exit.... see next comment
  17.     exit;
  18.   end
  19.   // ... or you can use the else statement
  20.   // else
  21.   // begin
  22.   //   here goes other onactivate code that you do not wish to perform
  23.   //   on the first time the handler is invoked
  24.   // end;
  25.  
  26.   // here goes other onactivate code that you (also) wish to perform
  27.   // for the first invocation of the event
  28. end;
  29.  

And in case you want very fancy then you can use an integer instead of a boolean value (increase it for every run) and annoy people with displaying a random message based on the value of the integer for example every 3th odd FormActivate or so  :)
I do not have to remember anything anymore thanks to total-recall.

incendio

  • Sr. Member
  • ****
  • Posts: 291
Re: [SOLVED] How to Show a form before app terminate?
« Reply #6 on: September 20, 2023, 08:34:52 am »
@incendio:
Glad to hear that the provided solution was able to help you out. You're welcome.

The example can be extended to fulfill other needs in case you might need it (in the future, non related to your current question):
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormActivate(Sender: TObject);
  2. const
  3.   // typed contant will keep its value for every invocation of this handler
  4.   // similar to as a global variable does.
  5.   FirstTime : boolean = true;
  6. begin
  7.   // In case you need to use the form activate event for other stuff as well
  8.   // then set the boolean value to false at the first invocation of the
  9.   // activate event
  10.   if FirstTime then
  11.   begin
  12.     FirstTime := false;
  13.     PerformLengthyInitializationOperationsOnlyOnce;
  14.  
  15.     // if you do not wish for the other onactivate code to be invoked
  16.     // then you can call exit.... see next comment
  17.     exit;
  18.   end
  19.   // ... or you can use the else statement
  20.   // else
  21.   // begin
  22.   //   here goes other onactivate code that you do not wish to perform
  23.   //   on the first time the handler is invoked
  24.   // end;
  25.  
  26.   // here goes other onactivate code that you (also) wish to perform
  27.   // for the first invocation of the event
  28. end;
  29.  

And in case you want very fancy then you can use an integer instead of a boolean value (increase it for every run) and annoy people with displaying a random message based on the value of the integer for example every 3th odd FormActivate or so  :)
Great, thanks for the tip.

Bookmarked it.

 

TinyPortal © 2005-2018