.NETGURU
timer doesn't tick
Messages   Related Types
This message was discovered on microsoft.public.dotnet.languages.vb.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.
Post a new message to this list...

mark (VIP)
Related to timer with subroutine requirement posted:9/11/2004 3:52 PM PST     

The sleep function worked well for the application.

I have another application where I want an elapsed time within a function.

A simple analog is provided below:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
timer1.Enabled = True
Dim i As Int32
Dim j As Int32
For i = 1 To 600000000
For j = 1 To 10
Next j
Next i
TextBox1.Text = count
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles timer1.Tick
count = count + 0.01
End Sub

The loop in the button1 sub takes about 20 seconds to complete on my computer.

The textbox shows count to be 0 when complete?

I am guessing that the timer needs to be on its own thread and that the
button click needs to raise an event which starts it.

What is the proper approach?

--
mark
Reply to this message...
 
    
Herfried K. Wagner [MVP] (VIP)
* "=?Utf-8?B?bWFyaw==?=" <Click here to reveal e-mail address> scripsit:
[Original message clipped]

Why not put that into a separate thread and access the textbox using 'Control.Invoke'?

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Reply to this message...
 
    
Cor Ligthert
Mark,

You have so many answers, the answer from Ken Dopierala Jr, is the right
answer on your direct question.

For the rest when you want to do time measurement than this is a very easy
one is because the one you use now is terrible insecure.

dim start as integer = enviroment.ticks
dim elapsedtiem = environment.ticks - start

(When used to repeatly show in your procedure you need to do a
textbox1.refresh as well in that)

This is on 1/1000 milliseconds (and even not secure) when you want it more
secure there are other methods however all better than the ones sugested in
this treads, really secure you get it never on a computer which does more
than only be a clock.

I hope this helps?

Cor

Reply to this message...
 
    
thomas wenning
"mark" <Click here to reveal e-mail address> schrieb im Newsbeitrag
news:Click here to reveal e-mail address...
[Original message clipped]

Hi mark,

look here http://www.codeproject.com/csharp/FTwin32Timers.asp

Greeting

Thomas

Reply to this message...
 
    
Ken Dopierala Jr.
Hi Mark,

It may be that the loop is locking the system and Windows can't process any
events. Try putting an Application.DoEvents() after the Next j line and see
if that gets the timer ticking. Note, this will also greatly slow down the
loop. Good luck! Ken.

"mark" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
    
Michael D. Ober
VB (all versions) won't check it's Windows Message Queue when running in a
tight (read - no blocking statements) loop. This prevents system messages,
of which the timer is one, from being read from the message queue. You need
to put a DoEvents inside either the for i ... loop or the For j ... loop.
It doesn't really matter which one.

Mike Ober.

"Ken Dopierala Jr." <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
    
Ken Dopierala Jr.
Hi Mike,

Your right. But if you put the DoEvents in the j loop then the loop will
run 10 times slower in this case. Ken.

"Michael D. Ober" <obermd.@.alum.mit.edu.nospam> wrote in message
news:eJJdo$Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
    
Imran Koradia (VIP)
What timer are you using? System.Windows.Forms.Timer runs on the same thread
as the application while the other 2 timers - System.Timers.Timer and
System.Threading.Timer - run on a different thread. So if you are using
System.Windows.Forms.Timer then you would run into this problem. Try using
System.Threading.Timer instead.

hope that helps..
Imran.

"mark" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
    
mark (VIP)
I think we're going in the right direction. However the two other times do
not appear to have tick events with which to increment the counter. Also, the
threading timer doesn't seem to have any events.
"Imran Koradia" wrote:

[Original message clipped]

Reply to this message...
 
    
Imran Koradia (VIP)
The Threading timer is used in a diffferent way. You provide a TimerCallback
delegate in the constructor of the timer (to specify the method you want to
execute when the interval elapses) and specify the time interval (as well as
the time to wait for the first execution) at which that method should
execute. In your case, System.Timers.Timer seems to fit better. You can use
the Elapsed event for this timer (similar to the tick event in the forms
timer).

hope that helps..
Imran.

"mark" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
 
System.EventArgs
System.Object
System.Threading.Timer
System.Threading.TimerCallback
System.Timers.Timer
System.Web.UI.Control
System.Windows.Forms.Application
System.Windows.Forms.Timer




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification