Solved

count down problem

Posted on 2006-10-30
10
297 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This video discusses moving either the default database or any database to a new volume.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 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

12 Experts available now in Live!

Get 1:1 Help Now