Solved

count down problem

Posted on 2006-10-30
10
299 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
  • 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 125 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Convert Jpg /PNG To GIF 5 120
Virtuailstring tree compare node issue 14 105
Create Database on Android via Delphi dbExpress 3 81
DBGrid or StringGrid ? 6 72
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 my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

863 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now