Recent

Author Topic: Error on Termination  (Read 5884 times)

MJ Haste

  • New Member
  • *
  • Posts: 34
  • An aspiring programmer!
    • MJHaste's YouTube
Error on Termination
« on: September 07, 2014, 02:37:45 pm »
Hi, I want to make this short so, I have a program (Countdown) which on it's final form has a 'Exit' button. Yet when it's pressed it seems to load up a copy of the final form, and randomly does what it should. I know Lazarus can be a bit glitchy and I want to know if that's the case or whether there's a fix.
Here's the code for my final form, if more information is needed just ask:
Code: [Select]
unit graphicaltimerp3;

{$mode objfpc}{$H+}

interface

uses Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  ExtCtrls, Crt, ShellApi, math, Variables, MMSystem;

type

  { TForm3 }

  TForm3 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Timer1: TTimer;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
  private
    { private declarations }
    Colour:Integer;
  public
    { public declarations }
  end;

var
  Form3: TForm3;

implementation

{$R *.lfm}

{ TForm3 }

procedure TForm3.Button1Click(Sender: TObject);
begin
  Loop:=0;
  Timed:=0;
  Hours:=0;
  Minutes:=50;
  Seconds:=0;
  Self.Close;
end;

procedure TForm3.Button2Click(Sender: TObject);
begin
  Timer1.Enabled:=False;
  Application.Terminate;
end;

procedure TForm3.FormCreate(Sender: TObject);
begin
  If ShellExecute(0,nil, PChar('"C:\Users\Morgan\Desktop\Programming\Pascal\Break Timer\Alarms\Tell Me.mp3"'),PChar('"C:\Users\Morgan\Desktop\Programming\Pascal\Break Timer\Alarms\Tell Me.mp3"'),nil,1) =0 then;
end;

procedure TForm3.Timer1Timer(Sender : TObject);
begin
  If (Colour=0) then
    Begin
      Color:=clAqua;
      Font.Color:=clFuchsia;
      Colour:=1;
    end
  else
    Begin
      Color:=clLime;
      Font.Color:=clRed;
      Colour:=0;
    end;
end;

end.   
Programming is my middle name! Wait, no it is not...

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Error on Termination
« Reply #1 on: September 07, 2014, 02:40:02 pm »
Sorry haven't looked at code etc but please always post your Lazarus version and operating system (see first link in my signature).  Sometimes it makes a difference.

Thanks

Edit:
Quote
randomly does what it should.
That's not very clear to me...
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

MJ Haste

  • New Member
  • *
  • Posts: 34
  • An aspiring programmer!
    • MJHaste's YouTube
Re: Error on Termination
« Reply #2 on: September 07, 2014, 02:53:54 pm »
Thanks.
Lazarus version: 1.2.4
Operating System: Windows 8.1

By 'randomly does what it should' I meant that it executed the code correctly at an unknown point in time. It seems to only execute the code correctly after a few attempts.
Programming is my middle name! Wait, no it is not...

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Error on Termination
« Reply #3 on: September 07, 2014, 02:59:55 pm »
So which one is the exit button? Button1 or button2?
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Error on Termination
« Reply #4 on: September 07, 2014, 03:22:58 pm »
In general when submitting code that does not work as intended, you should submit a complete compilable source that others can test. Otherwise forum readers can only guess at what constitutes the missing parts, which may be the very parts (unknown except to you) that cause the effect you are enlisting help for on this forum.

The recommended way to close a GUI application is to call Close on the main form (not to call Application.Terminate directly).

MJ Haste

  • New Member
  • *
  • Posts: 34
  • An aspiring programmer!
    • MJHaste's YouTube
Re: Error on Termination
« Reply #5 on: September 07, 2014, 03:35:02 pm »
Button 2 is the 'Exit' button.
Yeah, I thought I might of needed to post the whole program.
Also, as far I know I can't call Close on my main form from the final as it creates a loop (From declaring the forms in uses). I could use a timer on the main form that checks for a variable to be TRUE, but surely there's a better way.
Also, I forget to mention these events also occur when I click on the default close button, not just the button I've created.
Thanks.
Programming is my middle name! Wait, no it is not...

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Error on Termination
« Reply #6 on: September 07, 2014, 04:22:46 pm »
Pressing Exit button properly stops application for me at least.

Try replacing
Code: [Select]
with TForm3.Create(Application.MainForm) dowith
Code: [Select]
with TForm3.Create(Application) do
They both work for me, don't know why it doesn't for you.

MJ Haste

  • New Member
  • *
  • Posts: 34
  • An aspiring programmer!
    • MJHaste's YouTube
Re: Error on Termination
« Reply #7 on: September 07, 2014, 05:02:23 pm »
That made no difference, what Operating System do you have? Maybe it's just a problem with Windows 8.1!
Programming is my middle name! Wait, no it is not...

rvk

  • Hero Member
  • *****
  • Posts: 6162
Re: Error on Termination
« Reply #8 on: September 07, 2014, 07:25:22 pm »
Maybe it's just me... but what are you doing here?
Code: [Select]
procedure TForm1.Button9Click(Sender: TObject);
begin
  with TForm2.Create(Application.MainForm) do
  Begin
  Show;
  Self.Hide;
  RequireDerivedFormResource := True;
  Application.Initialize;
  Application.CreateForm(TForm2, Form2);
  Application.Run;
  end;
end;
You again call Application.Initialize and Application.Run while this is already called in GraphicTimer.lpr. Maybe it's just me and it's perfectly legal but i've never seen this done this way. (and aren't you creating two TForm2's, one unnamed and one in Form2?)

So why are you calling Application.Initialize and Application.Run again?

MJ Haste

  • New Member
  • *
  • Posts: 34
  • An aspiring programmer!
    • MJHaste's YouTube
Re: Error on Termination
« Reply #9 on: September 07, 2014, 08:18:45 pm »
Okay, I just removed them from GraphicalTimer. I'm very new to Pascal/Lazarus and I wasn't really aware of their functionality. Thanks for pointing out their necessity.

I don't understand what you mean about creating two TForm2's. From what I understand I only called for it's creation once.
Programming is my middle name! Wait, no it is not...

rvk

  • Hero Member
  • *****
  • Posts: 6162
Re: Error on Termination
« Reply #10 on: September 07, 2014, 08:38:04 pm »
I don't understand what you mean about creating two TForm2's. From what I understand I only called for it's creation once.

Here is the first creation of a TForm2 that does get shown (by the Show right after it) but doesn't have a reference to it.
Code: [Select]
  with TForm2.Create(Application.MainForm) do

Here would be the second creation of TForm2 which is referenced by Form2.
Code: [Select]
  Application.CreateForm(TForm2, Form2);

But as i said earlier... I don't think you are allowed to call Application.Initialize and .Run a second time. Maybe that's throwing the whole thing off.

This should be enough:
Code: [Select]
procedure TForm1.Button9Click(Sender: TObject);
begin
  with TForm2.Create(Application) do
  begin
    Show;
    Self.Hide;
  end;
end;

I also saw you did the Application.Initialize and .Run in the other units a few times too. You also need to remove them. After that you can remove all the "Formx: TFormx" variable in all units. The only one you really use is Form5: TForm5; in logo.pas. All the others can be removed because you never assign them.

So do all the TFormx.Create with Application as parameter "TForm2.Create(Application)" and remove all the RequireDerivedFormResource, Application.Initialize, Application.CreateForm and Application.Run lines (except in .lpr).
« Last Edit: September 07, 2014, 08:42:14 pm by rvk »

MJ Haste

  • New Member
  • *
  • Posts: 34
  • An aspiring programmer!
    • MJHaste's YouTube
Re: Error on Termination
« Reply #11 on: September 08, 2014, 07:33:39 pm »
Wow I'm literally amazed that that was the issue! Thanks a whole bunch!
It's now almost perfect. I might be back for some more help, but for the meantime thanks!
Programming is my middle name! Wait, no it is not...

 

TinyPortal © 2005-2018