?
Solved

count down problem

Posted on 2006-10-30
10
Medium Priority
?
314 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
9 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month8 days, 15 hours left to enroll

621 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