Answer the question
In order to leave comments, you need to log in
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;
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
Try to insert before the slip or after itApplication.ProcessMessages;
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 questionAsk a Question
731 491 924 answers to any question