T
T
Taras Serevann2014-05-22 17:40:14
.NET
Taras Serevann, 2014-05-22 17:40:14

Why does a Pascal program hang?

I am writing a timer in PascalABC.NET. When creating a project, I selected the windows forms project.
When you press the button, the program should start counting from 0 seconds to 999 with this code

var x:integer;
begin
  for x:=1 to 999 do
  begin
  sleep(60);
  label1.Text := inttostr(x);
  end;

but instead it freezes (windows error "application not responding"). Who knows how to fix?
PS: full program code
Unit Unit1;

interface

uses System, System.Drawing, System.Windows.Forms;

type
  Form1 = class(Form)
    procedure button1_Click(sender: Object; e: EventArgs);
  {$region FormDesigner}
  private
    {$resource Unit1.Form1.resources}
    button1: Button;
    label1: &Label;
    {$include Unit1.Form1.inc}
  {$endregion FormDesigner}
  public
    constructor;
    begin
      InitializeComponent;
    end;
  end;

implementation

procedure Form1.button1_Click(sender: Object; e: EventArgs);
  var x:integer;
begin
  for x:=1 to 999 do
  begin
  sleep(60);
  label1.Text := inttostr(x);
  end;
end;

end.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rostislav Grigoriev, 2014-05-22
@crazyzubr

Try to insert before the slip or after itApplication.ProcessMessages;

R
ragimovich, 2014-05-22
@ragimovich

I won’t say anything about Pascal, but something tells me that the countdown is carried out in the main thread of the program and blocks the GUI, which, when clicked in the window, is an “unresponsive” program in Windows terms. Take out the counter in a separate thread (thread) and you will be happy. And you can check my correctness by decreasing the counter to 5 seconds - after 5 seconds the program will start responding even with your code.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question