Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

is the TTimer what I need?? 1

Status
Not open for further replies.

Bamben

Programmer
Jul 22, 2009
70
IT
I have been learning delphi for 1 week now and I am trying to get further in my learning of Delphi, I have a large script that I am studying... so far I have learned to write data to an ini file, read data from and ini file, make an array, make a button do a procedure, I can do quite a few things now from studying the script.
I want to now move on to doing procedures that lead to other procedures so I am experimenting by creating the most simple of programs: hello world!

I have made a program that shows the message 'Hello world' after 10seconds of being open, but it does this every ten seconds over and over and over never stopping.

Can some one show me how to do this?


1.when my program opens you have a button that you can click.
2.when you click the button it shows the message 'hello world' after 10 seconds. it does this once then stops.
3.after 20 seconds it show another message 'why have you pulled all of your hair out?' it does this once and then stops.

If I can understand how to start and stop events when I need to, then my learning will vastly improve. as far as I understand at the moment I have to put conditions for things starting and stopping (if)... I would be happy with any help understanding this simple thing.
 
Code:
Timer1.Enabled := false;

Measurement is not management.
 
I can do that (Timer1.Enabled := false;) and then when the button is clicked it does Timer1.Enabled := True; but then how do I switch it off again??? It just shows the message over and over and over. how do you stop it once its started? and how does it know to then go to the next procedure after it has stopped the first one?

Thanks dude...I have spent about two days so far messing about with Timers trying lots of different combinations of code but I can not yet see the logic of how they work.
 
What if I Enable both timers at the same time (OnButton1Click). The first Timer contains a command to show the message 'Hello world' which happens after ten seconds.
The second Timer contains a command to show the message 'why have you pulled all of your hair out?' which happens after twenty seconds.

Also I could have a 3rd Timer that start also at the (OnButton1Click) and stops both the other two timers after 21secs.........#!;*^ nope thats not going to work because timer 1 would have show its message twice by then!

I dont get it!!!
 
I cant stop each timer with a new timer because that would mean that I am forever stopping timers with timers.
 
When you click the button you do "Timer1.Enabled := true;". Then in the event for the timer, you put your message box and then "Timer1.Enabled := false;", which will switch the timer event off.

Measurement is not management.
 
I remember Timers were a little confusing for me too when I started.

The will only fire when your application is idle - that is, when none of the code in your project is being executed, and it's basically sitting there waiting for the user to press a button on the form, or interact in some way. This is because they use the main application thread - thus preventing any of your code running at the same time as a timer event.

 
thanks...its very weird I have tried that before and just now also, it doesn't work for me! i am getting really good at programing now lol but NOTHING I DO WORKS!!!!

look at this for example:

//AUTO (version 1)
{
procedure TMainForm.AUTO(Sender: TObject);
label x0;
label x0b;
label x1;
begin
Goto x0;
x0: begin
MakeCallActionExecute(Self); //WHY DOES IT STOP HERE????
if Assigned(CurrentCall) then GoTo x0b;
x0b: begin
CurrentCall.OnHold;
if CurrentCall.OnHold then
Goto x1;
end;end;
x1: begin
if CurrentCall.OnHold then //Checks if the current call is onhold
MakeCallActionExecute1(Self);
if Assigned(CurrentCall) then
begin
CurrentCall.OnHold;
if CurrentCall.OnHold then
end;end
end;
}
//AUTO (version 2)

procedure TMainForm.AUTO(Sender: TObject);
label x0;
label x0b;
label x1;
begin
Goto x0;
if Assigned(CurrentCall) then GoTo x0b;
if CurrentCall.OnHold then Goto x1;
x0: begin
MakeCallActionExecute(Self); //WHY DOES IT STOP HERE????
x0b: begin
CurrentCall.OnHold;
end;end;
x1: begin
if CurrentCall.OnHold then //Checks if the current call is onhold
MakeCallActionExecute1(Self);
if Assigned(CurrentCall) then
begin
CurrentCall.OnHold;
//if CurrentCall.OnHold then //...start the next call, lable x2...
end;end
end;

all of the procedures are declared I am using code form the original code (all I'm doing is re-arranging it). I cant get past only being able to do one thing pre click I am damn sure that I DO understand how to program, but????? why doesn't any code I write work??

The timer is simple I DO understand it just doesn't play fair with me... does any one know why I cant seem to do aany thing?????????? I am perplexed
 
For starters, I highly recommend you never, ever, use labels and goto statements. There is always a way to code without them, and they only make your code confusing to follow, and possibly nightmarish to debug.

Again - I would never use them.

In your second version, the first statement is goto x0, meaning that the next two statements will not ever be executed.

I would recommend you rewrite your procedure without the labels and gotos and show us again if you still can't get it working.
 
I'm not sure what you're trying to achieve, but this may be what it should look like:

Code:
[b]procedure[/b] TMainForm.AUTO(Sender: TObject);
[b]begin[/b]
  [b]if[/b] Assigned(CurrentCall) [b]then[/b]
  [b]begin[/b]
    CurrentCall.OnHold;
  [b]end[/b]
  [b]else[/b] [b]if[/b] CurrentCall.OnHold [b]then[/b]
  [b]begin[/b]
    MakeCallActionExecute1(Self);
    [b]if[/b] Assigned(CurrentCall) [b]then[/b]
    [b]begin[/b]
      CurrentCall.OnHold;
    [b]end[/b];
  [b]end[/b]
  [b]else[/b]
  [b]begin[/b]
    MakeCallActionExecute(Self);
  [b]end[/b];
[b]end[/b];

I would usually omit most of the begin..end surrounds because Delphi doesn't require them if only one statement follows an if..then. But you can get all sorts of logic errors if you not careful and add extra statements later.
 
I should have looked at it more closely. What you've got there is a garble of logic that doesn't make any sense. But use the if..then..else blocks to get your code doing what you want.
 
*per click not pre click

I am basically programming new code now on my own, note that I don't get ANY errors (I have also got good at getting rid of errors!)

I know I am doing things correctly (as I am reading tutorials non stop on all different things so I get understanding quickly)

BUT NOTHING WORKS!!!!! I'm not miss spelling, I am copying and pasting code that's already written (weather it be code from tutorials, code from delphi, or code from my pre-written script)

Ps: the script works fine! I tested and backed it up before I started to play with it! It still works fine I just cant do much new ANYTHING REALLY!?

I GET zero errors! but all my code doesn't work I cant figure it out for the life of me I try method after method (each one until it looks neat and logical and I am happy that I have totally understood) but then as all the other methods i try THEY DONT WORK!!!!

I am not trying to perform a specific task here APART FROM FIGURING OUT WHY NOTHING NOTHING NOTHING will work!!????

I really need to get a grip.
 
I started off trying to do that with a case command ...like jumping from one step to the next conditionally. but that dint work ether IT DID ZIPPPPPP!
 
You would need to post the contents of the MakeCallActionExecute method for us to know why it was hanging.
 
procedure TMainForm.MakeCallActionExecute(Sender: TObject);
var
InDevice, OutDevice: Cardinal;
N: TTreeNode;
begin
if Assigned(CurrentAccount) then
begin
InDevice := AudioInDeviceNameToDeviceID(waveInDevice.Items[waveInDevice.ItemIndex]);
OutDevice := AudioInDeviceNameToDeviceID(waveOutDevice.Items[waveOutDevice.ItemIndex]);
C[0] := SimpleSip1.AddCall(CurrentAccount, PhoneEdit.Text, InDevice, OutDevice); //*1
C[0]._AddRef;
N := TreeView.Items.AddChild(CurrentRegNode, C[0].User);
N.Data := Pointer(C[0]);
N.ImageIndex := 5;
N.SelectedIndex := 5;
N.Parent.Expand(True);
TreeView.Selected := N;
TreeView.Repaint;
end;
end;
 
basically I am trying to set my self an exercise:

Make one call
hold that call
make another call
hold that call

because while playing with this phone script I saw that if you make two phone calls to two different people then only the CurrentCall (or newest most recent on the TTreeNode) has sound so to have a 'three way' chat you would first have to put the calls on hold and ring separately, then take them off hold both together (when they are in an array) then the array of calls has sound
 
what could I study to get good at creating a nice flow of events that don't get stuck?

To tell you the truth I am quite dyslexic (instead of reading 123 my mind reads 231, and dog would be odg. I tend to put the middle first, the end next and then the start some times 213 btw) this is just how I see things 'New York' is 'Yew Nork' lol. This is why I copy and paste, other wize my code would be all spelt like that (*pre is per).

Its not just limited to my spelling its every thing.

Like Griffyn said "What you've got there is a garble of logic that doesn't make any sense. But use the if..then..else blocks to get your code doing what you want."

I will get the methods switched round too!!

I have been looking at Griffyn's code and he does what I was doing in the first step LAST, my middle step he puts first and then step 2 is still step 2....

So I did infact flip the prodidure around in 'my usual style'... I need a head transplant really! is there any good matiral on the subject of doing things in a logical order??
 
I've always found the humble flowchart to be helpful in laying out a new program. Once set out, it allows you to think of every what/if scenario and run the program through the flowchart to see if it will run into problems, either with logic or scope.

Often, you can then design the program itself as a series of independent black boxes that interact. This model will be enormously helpful for future debugging, and when adding new features later on.
 
thanks Griffyn, I will research that till I understand.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top