Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

count down problem

Posted on 2006-10-30
10
Medium Priority
?
310 Views
Last Modified: 2010-04-05
I added a autoconnect funcation to my system but i have a small bug when its counting down it locks up the windows until it has finished counting down so my question is there a better way of count down then what I am using?

[code]
procedure TFirstForm.AutoTimerTimer(Sender: TObject);
var
i: Integer;
begin
i := 9;
repeat
 AutoLogin.Caption := 'Login Automatically('+inttostr(I)+')';
 application.processmessages;
 dec(i,1);
 sleep(1000);
until i = 0;
 AutoLogin.Caption := 'Login Automatically(0)';
 AutoTimer.Enabled := False;
 LoginButClick(nil);
end;

procedure TFirstForm.AutoLoginClick(Sender: TObject);
begin
if AutoLogin.Checked then
   AutoTimer.Enabled := True
  else
   AutoTimer.Enabled := false;
   AutoLogin.Caption := 'Login Automatically';
end;

this is my code its basic so am hoping there be a quick fix to this cheers
0
Comment
Question by:fiveuk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +1
10 Comments
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 17834416
Maybe something like this

procedure TFirstForm.AutoTimerTimer(Sender: TObject);
var
      i: Integer;
begin

      i := 9;
      while i > 0 do
      begin
            AutoLogin.Caption := 'Login Automatically('+inttostr(I)+')';
            AutoLogin.Refresh;
            AutoLogin.Invalidate;
            Dec( i)
      end;

      AutoLogin.Caption := 'Login Automatically(0)';
      AutoTimer.Enabled := False;
      LoginButClick(nil);
end;
0
 
LVL 6

Expert Comment

by:wimmeyvaert
ID: 17840129
It is the sleep-function that causes the 'lock'.

Better use a global variable (e.g. SleepSeconds), declared in your Private part of your Form.

Then use a TTimer and set its Interval to 1000 (ms).

Then in the OnTimer-Event you can place something like (code not tested ) :

AutoTimer.Enabled := False;
AutoLogin.Caption := 'Login Automatically (' + inttostr(SleepSeconds) + ')';
Dec( SleepSeconds );

if ( SleepSeconds <= 0 ) then
     LoginButClick(nil)
else
     AutoTimer.Enabled := True;


Best regards,

The Mayor.
0
 
LVL 6

Accepted Solution

by:
wimmeyvaert earned 500 total points
ID: 17840173
I made a little demo-app to show what you want.
I just placed a TButton ('Button1'), TEdit ('Edit1') and TTimer ('AutoTimer') on the Form.
TButton-Click resets the Timer.
TEdit contains the amount of seconds before action will take place.

Her is the complete code (.Pas) :

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls, StdCtrls;

type
  TForm1 = class(TForm)
    AutoTimer: TTimer;
    Button1: TButton;
    Edit1: TEdit;
    procedure AutoTimerTimer(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    SleepSeconds: Integer;
    procedure SetTimerProps;
    procedure UpdateFormCaption( Sec: Integer );
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
  SetTimerProps;  // Set initial Timer-Properties
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  SetTimerProps; // Set Timer-properties manually.
end;

// Is triggered every second (AutoTimer.Interval = 1000).
procedure TForm1.AutoTimerTimer(Sender: TObject);
begin
  AutoTimer.Enabled := False; // Disable Timer.
  // Now update the Form's Caption (countdown).
  UpdateFormCaption( SleepSeconds );
  // Decrease the Second-Counter.
  Dec( SleepSeconds );
  // If Counter becomes less than zero, do action.
  if ( SleepSeconds < 0 ) then
    ShowMessage( 'LoginButClick(nil) ' )  // Do action
  else
     AutoTimer.Enabled := True;  // Re-enable Timer.
end;


procedure TForm1.SetTimerProps;
begin
  // set global variable to users value from Edit1.
  SleepSeconds := StrToInt( Edit1.Text );
  // Update Form's Caption (CountDown).
  UpdateFormCaption( SleepSeconds );
  // Enable Timer to start CountDown.
  AutoTimer.Enabled := True;
end;

procedure TForm1.UpdateFormCaption(Sec: Integer);
begin
  // Update Form's Caption to current Counter.
  Self.Caption := 'Login Automatically in (' + inttostr( Sec ) + ') seconds';
end;

end.



Hope this clears out what I mean.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:fiveuk
ID: 17841001
it dont count down it just logins in soon as I load program needs to count from 10 to 0 then login!
0
 
LVL 6

Expert Comment

by:wimmeyvaert
ID: 17841249
Yes, but in your Timer's Code I can see you loop 10 times (1 time per second) and just decreases the counter.
In this loop you use the Sleep-function (wich blocks the application).
And then , after your loop you call the Login-Function.

So, my code does approx. the same, isn't it ?

0
 

Author Comment

by:fiveuk
ID: 17841505
would I change SleepSeconds := StrToInt( Edit1.Text );

to

SleepSeconds := StrToInt('1000');?? or just 10
0
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 17845252
I dont understand why you need the sleep function at all.
The timer can count to 10 by itself and the application doesnt have to do anything.
0
 
LVL 6

Expert Comment

by:wimmeyvaert
ID: 17856898
Hi,

@fiveuk : Set the Interval property of your Timer to 1000 (=1 second), and set the SleepSeconds = 10.


@MikeLittleWood : I guess it is to be able to interact with the user.
You could set the Timer.Interval to 10000 (10 seconds), but then you can't show a sountdown-counter on the form.
If you set the Interval to 1000 (= second), then the OnTimer-Event is triggered every second. Then you can decrease the Counter and show this counter on the Form, so that the user knows how much seconds are left before action will take place.

0
 
LVL 1

Expert Comment

by:Computer101
ID: 21133072
Forced accept.

Computer101
EE Admin
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question