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 Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Timer ticks 2 times?

Status
Not open for further replies.

astrodestino

IS-IT--Management
Feb 19, 2005
179
AR
Hi!
my code:

Private Sub timer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles timer.Tick
counter=counter+1
End Sub

Timer interval its 1000

the thing is that every interval counter is counter+2 not +1

Y added a msgbox and its ok but every interval the timer will execute two times counter=counter+1 so if I want to trigger some action when couter = 10 I have to trigger it when counter=20 if not it will be triggered too soon.
Can that be fixed?
Tnx!
 
I'd be suspisious of the 2nd tick. There shouldn't be a way for the tick event to fire twice for one tick. is it possible that the code in the tick event can take a second or longer to run?

-Rick

----------------------

[monkey] I believe in killer coding ninja monkeys.[monkey]
 
counter is declared like:

Public Counter as integer (in a module)

Is counter is delared in the form I got no problems but if it is declared in a module it will add 2 instead of one.
Any ideas? :S
 
try this in the module:
Code:
Private Counter as integer

Some other even is accessing the PUBLIC Counter variable. You only need it to be accessed by the object the timer is on, so you can declare it as PRIVATE in that class.

-Rick

----------------------

[monkey] I believe in killer coding ninja monkeys.[monkey]
 
I checked int timer tick I rem counter=counter +1 and counter remains at 0 so nothing is adding other values to it.
If a move the public counter as integer to the form that holds the counter I got no problems but why I must have it here, can I have it in a module like in vb6?
 
no but there is also other code inside the timer that will react depending on the counter value and it will be trigered 2 times :(
 
dont worry guys I fixed by adding the timers in other hidden form and triggering the actions from there.
Dont know about you but, as I said to MS, vb net sucks, everything was easy in VB6 and worked, here I type more code, runs slower, and works if vb.net wants.
Tnx for the help anyway and excuse me about the comments, its only I'm very andgry about forcing me to code in .net (the twilight zone).
 
Dont know about you but, as I said to MS, vb net sucks, everything was easy in VB6 and worked, here I type more code, runs slower, and works if vb.net wants.
Nope...I find the exact opposite true.

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
here I type more code
Also, I'd be very suprised if this were true. The amount of classes that .NET has provided that dramitically reduce d the lines of code taken to perform a simple task is unbelievable.


----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
dont worry guys I fixed by adding the timers in other hidden form and triggering the actions from there.

This sounds like the working definition of a "Kludge". Or an inelligant, non-robust, hope it works, kind of way to solve a problem. Kludges are know for providing flakey results, poor performance, hard debugging, and extended maintenance.

Dont know about you but, as I said to MS, vb net sucks,

I completely disagree. VB.Net IS NOT VB7. VB.Net is CLR w/ a VB like interface and syntax.

everything was easy in VB6 and worked,

Some things were easier in VB6. But building large robust application was significantly harder, standardization was much more difficult, inheritance was non-existant... VB.Net has major advantages over VB6 and few drawbacks.

here I type more code, runs slower, and works if vb.net wants.

VB.Net takes less code (if you use inheritance and OO design), runs at about the same speed, and does exactly what you tell it to do (just like VB6). It is also Object Oriented, which offers significant benefits in design and development time. We just brought a full blown reporting application from the design board to implimentation in under 5 months. We could do that because of the OO nature of VB.Net and Inheritance. Writing the same app in VB6, we may have pulled it off in 6 months, but there would be little to no standarization and maintenance would be a nightmare.

Tnx for the help anyway and excuse me about the comments, its only I'm very andgry about forcing me to code in .net (the twilight zone).

I know some COBOL programmers who used to say the same thing. You'll note how few COBOL programmers sill exist today.

-Rick

----------------------

[monkey] I believe in killer coding ninja monkeys.[monkey]
 
I wouldn't consider that to be much of a reason to think that "vb.net sucks"!

Try binding any sort of data (such as a Collections or an array) to a list in VB6 compared to VB.NET (and seeing how objects such as a DataTable or DataSet didn't even exist in VB6!) and you'll find the amount of code taken reduces dramatically.

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
Rick, 2 things, this app is running without problems in vb6, so you tell me, how if I have the same code (no apis and things like that, only odbc) in vb6 timer ticks and actions is trigeres once for every single tick and in vb.net 2. This is nos serius, you can build a vb.net flag and attach it to the highest building in new york but you have to relize that 1+1 is 2 in c++, coboo, delphi, pascal, vb6 and must be 2 in vb.net, only that the same simple code, without apis and strange things like that, that works in vb6 is not working vb.net.
i dont want to fight with anyone but I lost hours with a stupid timer thing, thats a reason to be angry with vb net and more if the same app in vb6 is working!
 
Try opening 3 copies of form1 and tracking data on each one in VB6 vs VB.Net. I can assure you the solution would be much cleaner in .Net.

The reason why you think VB.Net sucks is because you are expecting it to be VB7. It isn't. VB.Net take an entirely new approach and design methodology. Once you "get" Object Oriented coding, VB.Net will seem far supirior to VB6.

-Rick

----------------------

[monkey] I believe in killer coding ninja monkeys.[monkey]
 
astrodestino,

You've made an incorrect assumption.

VB.NET acts like any other programming language in that it only does what it is told.

Add a Timer and a Label to a form and add the following code:
Code:
    Dim i As Integer = 0
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Label1.Text = i
        i += 1
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Timer1.Start()
    End Sub
You'll notice that the i integer increases by one each time as expected and therefore both the Timer control and the .NET code work perfectly.

If your code is increasing by 2 it is because it is being told to. I can't see where from what you have posted but it doesn't do it by default as I have proved above.

I think that most of the MVP's here (if not all) will agree that there isn't a drastic design flaw in VB.NET and we can get simple things such as this working (meaning that it isn't a problem with .NET but how it is implemented by the "programmer" or "developer").

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
Here's why I beleive this is a mistake on your part Astro:

no i only use that code that you see...

no but there is also other code inside the timer that will react depending on the counter value

You never posted what that code was. and as ca8 said, Vb.Net will do exactly what you tell it to do. Just like all programming languages. In the history of VB.Net I have never heard of a bug where the timer.tick event fires twice. It works perfectly fine for thousands of other developers. So if your counter is being incremented twice, it's because something in your code is causing it.

-Rick

----------------------

[monkey] I believe in killer coding ninja monkeys.[monkey]
 
No I never posted because it wasnt important, was something like:

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
counter=counter+1
if counter = 10 then
timer1.enable=false
msgbox counter
myevent()
do
application.doevents()
loop until ok=true
timer1.enable=true
counter = 0
end if
end sub

There were 2 msgboxes instead of one.

I can asure you rick that there is nothing incrmenting the counter. There you have my code. There is nothing there that can increase the counter by two, and as I said the msgbox will popup twice!
I know is crazy but its happening. I openes a new solution I put a timer on it and it works but there is something inside this solution, dont know what it is whats making this crazy thing only in form1. Form1 only has timers and menues, form1 is the user interface where the user will trigger actions that are being processed in other forms... why is doing this? dont know...

If you make the app wait in the timer1.enabled=false code it will pause 2 times, first time then one second later (timer interval=1000) will pause again, and how can that be if timer1.enabled=false?
Is crazy, and as I said, there is no big coding in this form, only dropdown menues and the timer.... :(
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top