* * *

Author Topic: Windows - Application won't come out of the background  (Read 1480 times)

bigtunacan

  • Newbie
  • Posts: 4
Windows - Application won't come out of the background
« on: August 07, 2017, 04:59:20 am »
I created a basic form application; it compiles fine, but when I run it the application goes into the background and I am not able to activate and bring it to the foreground.  This is running on Windows 10.

RAW

  • Sr. Member
  • ****
  • Posts: 411
Re: Windows - Application won't come out of the background
« Reply #1 on: August 07, 2017, 05:06:58 am »
 ???  :)

..in the background ??? what does that mean exactly...
Do you have a little example.. some code, can you recreate that behaviour (sample program)... would be much easier to know what you are talking about... :)
Windows 7 Pro (x64 Sp1) And Windows XP Pro (x86 Sp3) - LAZARUS 1.6.4 FPC 3.0.2

bigtunacan

  • Newbie
  • Posts: 4
Re: Windows - Application won't come out of the background
« Reply #2 on: August 07, 2017, 05:17:17 am »
I have included the code below.  On Windows 10 you can do a swipe gesture to see what applications are running; this will show that my application is running as it displays the form applications in the swipe window, but the application can not be brought into the active foreground.

Code: Pascal  [Select]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  9.   ActnList;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     Button1: TButton;
  17.     Button2: TButton;
  18.     procedure Button1Click(Sender: TObject);
  19.     procedure Button2Click(Sender: TObject);
  20.   private
  21.     { private declarations }
  22.   public
  23.     { public declarations }
  24.   end;
  25.  
  26. var
  27.   Form1: TForm1;
  28.  
  29. implementation
  30.  
  31. {$R *.lfm}
  32.  
  33. { TForm1 }
  34.  
  35. procedure TForm1.Button2Click(Sender: TObject);
  36. begin
  37.   Close;
  38. end;
  39.  
  40. procedure TForm1.Button1Click(Sender: TObject);
  41. begin
  42.   Button1.Caption := 'Press again.';
  43. end;
  44.  
  45. end.
  46.  

RAW

  • Sr. Member
  • ****
  • Posts: 411
Re: Windows - Application won't come out of the background
« Reply #3 on: August 07, 2017, 05:29:52 am »
Maybe your program is not responsible for that...
I don't see why a Close and a Caption change should be any kind of a problem... but I don't know Windows 10...
Windows 7 Pro (x64 Sp1) And Windows XP Pro (x86 Sp3) - LAZARUS 1.6.4 FPC 3.0.2

Thaddy

  • Hero Member
  • *****
  • Posts: 4247
Re: Windows - Application won't come out of the background
« Reply #4 on: August 07, 2017, 08:50:19 am »
Use hide and show or the visible property, not close: close really closes the form. You need to recreate it. (dead giveaway is there is no equivalent open.)
On startup you can force Application.ShowMainForm := true;
See e.g. http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/devwin32/hidingthemainform_xml.html
« Last Edit: August 07, 2017, 09:01:50 am by Thaddy »
"Logically, no number of positive outcomes at the level of experimental testing can confirm a scientific theory, but a single counterexample is logically decisive."

RAW

  • Sr. Member
  • ****
  • Posts: 411
Re: Windows - Application won't come out of the background
« Reply #5 on: August 07, 2017, 09:09:35 am »
Yeah.. "Close" really sets the main form to the "background"...   the wood for the trees... I couldn't see this... Chapeau.
Windows 7 Pro (x64 Sp1) And Windows XP Pro (x86 Sp3) - LAZARUS 1.6.4 FPC 3.0.2

PatBayford

  • Jr. Member
  • **
  • Posts: 58
Re: Windows - Application won't come out of the background
« Reply #6 on: August 07, 2017, 10:05:26 pm »
As an aside, to actually terminate an application, it is best to use Application.Terminate, which will close all active forms, and terminate the top level application code.
Lazarus 1.6.4 FPC 3.0.2 SVN 54278 Windows 10 64bit

Thaddy

  • Hero Member
  • *****
  • Posts: 4247
Re: Windows - Application won't come out of the background
« Reply #7 on: August 07, 2017, 10:41:50 pm »
As an aside, to actually terminate an application, it is best to use Application.Terminate, which will close all active forms, and terminate the top level application code.
Not such good advice, actually it is very, very bad advice:
As a second aside: no, that is not the case, because application.terminate will ignore OnClose and OnCloseQuery events and shit happens... as we know....It just frees all resources and then halts.
The best way is to use OnCloseQuery first. That can - properly used - guarantee termination in a proper manner, like saving open buffered files, like in an editor first.....or not committed data...<bit grumpy >:D >:D >:D >:D>
Remind me to do a wiki entry on that.... O:-)

Never call application.terminate directly (although I wrote it is an option myself above, mea culpa <grumpy on myself  >:D >:D>) ... It's useless in a serious application, unless your program is already in an exception state for some reason. (Or your application should not persist any data, e.g. a calculator or something).
« Last Edit: August 07, 2017, 10:58:18 pm by Thaddy »
"Logically, no number of positive outcomes at the level of experimental testing can confirm a scientific theory, but a single counterexample is logically decisive."

RAW

  • Sr. Member
  • ****
  • Posts: 411
Re: Windows - Application won't come out of the background
« Reply #8 on: August 08, 2017, 03:12:42 am »
I don't see the problem here...
I use it a lot like this:
Code: Pascal  [Select]
  1. UNIT Unit1;
  2. {$MODE OBJFPC}{$H+}{$J-}
  3.  
  4. Interface
  5.  USES
  6.   Classes, SysUtils, Forms, Controls, StdCtrls;
  7.  
  8.  TYPE
  9.   TForm1 = Class(TForm)
  10.  
  11.    Button1: TButton;
  12.    AppProp: TApplicationProperties;
  13.  
  14.    Procedure FormCloseQuery    (Sender: TObject; Var CanClose: Boolean);
  15.    Procedure Button1Click      (Sender: TObject);
  16.    Procedure AppPropEndSession (Sender: TObject);
  17.  
  18.     PRIVATE
  19.      Procedure SaveAndQuit;
  20.   End;
  21.  
  22.  VAR
  23.   Form1: TForm1;
  24.  
  25. Implementation
  26. {$R *.LFM}
  27.  
  28.  
  29. Procedure TForm1.FormCloseQuery(Sender: TObject; Var CanClose: Boolean);
  30.  Begin
  31.   CanClose:= False;
  32.  End;
  33.  
  34.  
  35. Procedure TForm1.SaveAndQuit;
  36.  Begin
  37.   // SAVE whatever you have to save...
  38.   // FREE whatever you have to free...
  39.   Application.Terminate;
  40.  End;
  41.  
  42.  
  43. Procedure TForm1.Button1Click(Sender: TObject);
  44.  Begin
  45.   SaveAndQuit;
  46.  End;
  47.  
  48.  
  49. Procedure TForm1.AppPropEndSession(Sender: TObject);
  50.  Begin
  51.   SaveAndQuit;
  52.  End;
  53.  
  54. END.

Code: Pascal  [Select]
  1. PROGRAM project1;
  2. {$MODE OBJFPC}{$H+}{$J-}
  3.  
  4.  USES
  5.   {$IFDEF UNIX}{$IFDEF UseCThreads}
  6.    cthreads,
  7.   {$ENDIF}{$ENDIF}
  8.    Interfaces,
  9.    Forms,
  10.    Unit1;
  11.  
  12.   {$R *.RES}
  13.  
  14.  
  15. BEGIN
  16.  RequireDerivedFormResource:= True;
  17.   Application.Initialize;
  18.    Form1:= TForm1.Create(Application); // don't want a button
  19.    Form1.Show;
  20.   Application.Run;
  21. END.

Especially if I need a launcher or menu or something in the background that needs to stay there as long as the computer runs...
Of course there are other ways to do this. For example:

Code: Pascal  [Select]
  1. Procedure TForm1.FormClose(Sender: TObject; Var CloseAction: TCloseAction);
  2.  Begin
  3.   If Not booExit
  4.   Then CloseAction:= caNone;
  5.  End;

Code: Pascal  [Select]
  1. Procedure TForm1.FormCloseQuery(Sender: TObject; Var CanClose: Boolean);
  2.  Begin
  3.   CanClose:= booExit;
  4.  End;

BTW: The 2 examples only work with
Code: Pascal  [Select]
  1. Application.CreateForm(TForm1, Form1);
...
« Last Edit: August 08, 2017, 03:27:09 am by RAW »
Windows 7 Pro (x64 Sp1) And Windows XP Pro (x86 Sp3) - LAZARUS 1.6.4 FPC 3.0.2

Thaddy

  • Hero Member
  • *****
  • Posts: 4247
Re: Windows - Application won't come out of the background
« Reply #9 on: August 08, 2017, 07:39:55 am »
You misunderstand ClloseQuery, I editted your first unit a bit, no need for application.terminate:
Code: Pascal  [Select]
  1. UNIT Unit1;
  2. {$MODE OBJFPC}{$H+}{$J-}
  3.  
  4. Interface
  5.  USES
  6.   Classes, SysUtils, Forms, Controls, StdCtrls;
  7.  
  8.  TYPE
  9.   TForm1 = Class(TForm)
  10.  
  11.    Button1: TButton;
  12.    AppProp: TApplicationProperties;
  13.  
  14.    Procedure FormCloseQuery    (Sender: TObject; Var CanClose: Boolean);
  15.    Procedure Button1Click      (Sender: TObject);
  16.    Procedure AppPropEndSession (Sender: TObject);
  17.  
  18.     PRIVATE
  19.      Procedure SaveAndQuit;
  20.   End;
  21.  
  22.  VAR
  23.   Form1: TForm1;
  24.  
  25. Implementation
  26. {$R *.LFM}
  27.  
  28.  
  29. Procedure TForm1.FormCloseQuery(Sender: TObject; Var CanClose: Boolean);
  30.  Begin
  31.   SaveAndQuit; /// call it here....
  32.   CanClose:= True;  // application will terminate
  33.  End;
  34.  
  35.  
  36. Procedure TForm1.SaveAndQuit;
  37.  Begin
  38.   // example code:
  39.   // If Memo1.modified then AskForSaveBeforeQuite;
  40.   // SAVE whatever you have to save...
  41.   // FREE whatever you have to free...
  42.   // Superflous code ....Application.Terminate is not necessary when you handle CloseQuery correctly. Is is actually wrong if CanClose becomes true..
  43.  End;
  44.  
  45.  
  46. Procedure TForm1.Button1Click(Sender: TObject);
  47.  Begin
  48.   SaveAndQuit;
  49.  End;
  50.  
  51.  
  52. Procedure TForm1.AppPropEndSession(Sender: TObject);
  53.  Begin
  54.   SaveAndQuit;
  55.  End;
  56.  
  57. END.

I would also use a TrySaveAndQuit:Boolean; function and use that to determine CanClose. The user can then continue to work if he cancels the save dialog.
« Last Edit: August 08, 2017, 07:46:26 am by Thaddy »
"Logically, no number of positive outcomes at the level of experimental testing can confirm a scientific theory, but a single counterexample is logically decisive."

Thaddy

  • Hero Member
  • *****
  • Posts: 4247
Re: Windows - Application won't come out of the background
« Reply #10 on: August 08, 2017, 09:09:30 am »
@RAW
Here's a simple example how it should be done:
Code: Pascal  [Select]
  1. unit Unit1;
  2. {$mode objfpc}{$H+}
  3. interface
  4.  
  5. uses
  6.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, Menus,
  7.   StdCtrls;
  8.  
  9. type
  10.   { TForm1 }
  11.   TForm1 = class(TForm)
  12.     MainMenu1: TMainMenu;
  13.     Memo1: TMemo;
  14.     mnuKill: TMenuItem;
  15.     mnuFile: TMenuItem;
  16.     mnuSave: TMenuItem;
  17.     mnuExit: TMenuItem;
  18.     SaveDialog1: TSaveDialog;
  19.     procedure FormCloseQuery(Sender: TObject; var CanClose: boolean);
  20.     procedure mnuSaveClick(Sender: TObject);
  21.     procedure mnuExitAppClick(Sender: TObject);
  22.     function DoSave:Boolean;
  23.     procedure mnuKillSwitch(Sender: TObject);// demo's the pitfalls of calling application terminate directly
  24.   private
  25.   public
  26.   end;
  27.  
  28. var
  29.   Form1: TForm1;
  30.  
  31. implementation
  32. uses lcltype;
  33. {$R *.lfm}
  34. { TForm1 }
  35. procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: boolean);
  36. begin
  37.   CanClose := true;
  38.   if Memo1.Modified then
  39.     if Application.MessageBox('Do you want to save your work?','Text is modified',MB_YESNO) = idYes then
  40.       CanClose := DoSave;
  41. end;
  42.  
  43. procedure TForm1.mnuSaveClick(Sender: TObject);
  44. begin
  45.    DoSave;
  46. end;
  47.  
  48. procedure TForm1.mnuExitAppClick(Sender: TObject);
  49. begin
  50.   Application.MainForm.Close;
  51. end;
  52.  
  53. function TForm1.DoSave: Boolean;
  54. begin
  55.   Result := False;
  56.   if SaveDialog1.Execute then
  57.   begin
  58.     Memo1.Lines.SaveToFile(SaveDialog1.FileName);
  59.     Memo1.Modified := false;
  60.     Result := true;
  61.   end;
  62. end;
  63.  
  64. procedure TForm1.mnuKillSwitch(Sender: TObject);
  65. begin
  66.   Showmessage('Never do this, you will loose your work!');
  67.   Application.Terminate;
  68. end;
  69.  
  70. end.

- if text is modified, ask to save or not.
- also let's the user still cancel from the save dialog
- no explicit call to application terminate, because that will exit immediately and gives the user no option but to loose their work. (try it!, modify memo and kill)
« Last Edit: August 08, 2017, 11:44:11 am by Thaddy »
"Logically, no number of positive outcomes at the level of experimental testing can confirm a scientific theory, but a single counterexample is logically decisive."

RAW

  • Sr. Member
  • ****
  • Posts: 411
Re: Windows - Application won't come out of the background
« Reply #11 on: August 08, 2017, 11:48:31 am »
@Thaddy
Thank you very much for the example...
But
Code: Pascal  [Select]
  1. Application.MainForm.Close;
only works in combination with
Code: Pascal  [Select]
  1. Application.CreateForm(TForm1, Form1);

Without CreateForm I get an exception (SIGSEGV)...
Code: Pascal  [Select]
  1. RequireDerivedFormResource:= True;
  2.   Application.Initialize;
  3.    Form1:= TForm1.Create(Application); // don't want a button
  4.    Form1.Show;
  5.   Application.Run;
Windows 7 Pro (x64 Sp1) And Windows XP Pro (x86 Sp3) - LAZARUS 1.6.4 FPC 3.0.2

Thaddy

  • Hero Member
  • *****
  • Posts: 4247
Re: Windows - Application won't come out of the background
« Reply #12 on: August 08, 2017, 01:10:37 pm »
Of course. But my code works with the default....Don't nitpick. We are teaching people and I can outsmart almost everyone (by accident?  ;D O:-)).
The default is:
Code: Pascal  [Select]
  1. program Project1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   {$IFDEF UNIX}{$IFDEF UseCThreads}
  7.   cthreads,
  8.   {$ENDIF}{$ENDIF}
  9.   Interfaces, // this includes the LCL widgetset
  10.   Forms, unit1
  11.   { you can add units after this };
  12.  
  13. {$R *.res}
  14.  
  15. begin
  16.   RequireDerivedFormResource:=True;
  17.   Application.Initialize;
  18.   Application.CreateForm(TForm1, Form1); // <----???? Why did you ask? Confuses noobs! Of course that is needed.....
  19.   Application.Run;
  20. end.
« Last Edit: August 08, 2017, 01:44:49 pm by Thaddy »
"Logically, no number of positive outcomes at the level of experimental testing can confirm a scientific theory, but a single counterexample is logically decisive."

Thaddy

  • Hero Member
  • *****
  • Posts: 4247
Re: Windows - Application won't come out of the background
« Reply #13 on: August 08, 2017, 01:46:44 pm »
Without CreateForm I get an exception (SIGSEGV)...

Sigh...... (that's worse than grumpy)
"Logically, no number of positive outcomes at the level of experimental testing can confirm a scientific theory, but a single counterexample is logically decisive."

RAW

  • Sr. Member
  • ****
  • Posts: 411
Re: Windows - Application won't come out of the background
« Reply #14 on: August 08, 2017, 02:26:02 pm »
There is nothing wrong with "Application.Terminate"...
A normal program doesn't need it, but in some cases you really need it otherwise it wouldn't work. And exactly that shows my example and btw. I don't say everybody should do it that way, it's just an example...

Quote
Sigh...... (that's worse than grumpy)
No it's not.. I stick to my example that's all...

Quote
....Don't nitpick.
What do you think is this:
Quote
procedure TForm1.mnuKillSwitch(Sender: TObject);
begin
  Showmessage('Never do this, you will loose your work!');
  Application.Terminate;
end;
If you don't save you will loose your work... that has nothing to do with "Application.Terminate", if you use "Close" you loose it too...

Quote
...and I can outsmart almost everyone (by accident?  ;D O:-)).
You are confusing outsmart with exactness !!!
As I said before I stick to my example... that's it...
Windows 7 Pro (x64 Sp1) And Windows XP Pro (x86 Sp3) - LAZARUS 1.6.4 FPC 3.0.2

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus