473,396 Members | 1,726 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

problem with timer

hi plz help me, i would like to know if got two timer in form( vb), does it run parallel or synchronous. i got problem here where i want to calculate time duration the formula is EndTime-StartTime. I code the StartTime in first timer and the EndTime in second Timer(Timer2) with 5s and 10s Interval each. BUT i facing problem here where the Time Duration is inaccurate eventhough the formula is correct. I created array of 4 for startTime n End Time. Plz guide me to solve this problem.

Private Sub Timer1_Timer()
Randomize

lblCar(n).Caption = Int(Rnd() * 50)

StartTime = Now
lblStart(n).Caption = Format(StartTime, "hh:mm:ss")

n = n + 1
If n = 4 Then
Timer1.Enabled = False
End If

End Sub

Private Sub Timer2_Timer()


EndTime = Now
lblEnd(i).Caption = Format(EndTime, "hh:mm:ss")
Timeduration = DateDiff("s", StartTime, EndTime)
lblDuration(i).Caption = "" & Timeduration

i = i + 1
If i = 4 Then
Timer2.Enabled = False
End If

End Sub
Mar 12 '08 #1
14 1501
kadghar
1,295 Expert 1GB
hi plz help me, i would like to know if got two timer in form( vb), does it run parallel or synchronous. i got problem here where i want to calculate time duration the formula is EndTime-StartTime. I code the StartTime in first timer and the EndTime in second Timer(Timer2) with 5s and 10s Interval each. BUT i facing problem here where the Time Duration is inaccurate eventhough the formula is correct. I created array of 4 for startTime n End Time. Plz guide me to solve this problem.
I think you're not using timers properly. Timers will do whatever you write in them every time the interval is reached.
e.g
if your timer event says:

Expand|Select|Wrap|Line Numbers
  1. textbox1.text = textbox1.text + 1
And the interval is set to 1000, then each second, the textbox will increase by one. ^.^ Hope you can figure out how to get what you need, now.
Mar 12 '08 #2
Killer42
8,435 Expert 8TB
Ignore this message. I'm just "subscribing" to the discussion thread so I can see where it goes. :)
Mar 12 '08 #3
I think you're not using timers properly. Timers will do whatever you write in them every time the interval is reached.
e.g
if your timer event says:

Expand|Select|Wrap|Line Numbers
  1. textbox1.text = textbox1.text + 1
And the interval is set to 1000, then each second, the textbox will increase by one. ^.^ Hope you can figure out how to get what you need, now.
.

Sorry i couldn't understand ur code. i try out ur code but i get error msg said type mismatch n variable not define even though i have declare the variable. if u look at my code i got two timer. each timer i set to 5000 n 10000 interval. Is it ok to use two timer in one form? i getting wrong time duration even though the formula is correct. plz help me.
Mar 12 '08 #4
Killer42
8,435 Expert 8TB
I haven't been following this thread in detail. But I suspect your problem is to do with the way the timers work. They are independent. So one of them will be firing every (roughly) 5 seconds, and the other every (roughly) ten seconds.

In other words, Timer1 will trigger twice as often as Timer2. Is this what you intend?
Mar 12 '08 #5
kadghar
1,295 Expert 1GB
You said your first timer has a 5000ms interval
so each 5 seconds this will happen:
1. It'll change the seed of the random num generator.
2. It'll change the caption of the nth label.
3. It'll set the start time to now, with a convinient format. (are you sure you want to change the start time each 5 seconds?)
4. add 1 to n, and if n = 4, it will be disabled.

Timer2 will do this each 10 seconds:
1. Change the end time (are you sure you want to change it each 10 secs?)
3. Change the ith lblEnd
2. Change Timeduration (since the other timer is changed each 5 secs, it'll be zero)
3. Change the ith lblDuration
4. Increase i and if its 4, stop.

So... i'll assume both are enabled since the begining, and lets say you start your program at time 0, this is what you'll have:

Time 5 (tick Timer1)
i=0 , n = 1, StartTime=5, EndTime=0, Duration = 0
Time 10 (tick both)
i=1, n=2, StartTime=10, EndTime=10, Duration = 0 (it can be 5 sometimes)
Time 15 (tick Timer1)
i=1, n=3, starttime=15, EndTime=10, Duration = 0
Time 20 (tick both)
i=2, n=4, starttime= 20, EndTime=20, Duration = 0 (it could be 5 here too)
Time 30 (tick Timer2)
i=3, n=4, starttime=20, Endtime= 30, Duration = 10
Time 40 (tick Timer2)
i=4, n=4, starttime=20, Endtime=40, Duration = 20

Now, i didnt understand exactly what you wanted. About my example, well, it was only an example, it shouldnt be a solution.
I hope this can be of help, but please let us know if you have doubts, or if we can be of some help.
Mar 12 '08 #6
pureenhanoi
175 100+
hi plz help me, i would like to know if got two timer in form( vb), does it run parallel or synchronous. i got problem here where i want to calculate time duration the formula is EndTime-StartTime. I code the StartTime in first timer and the EndTime in second Timer(Timer2) with 5s and 10s Interval each. BUT i facing problem here where the Time Duration is inaccurate eventhough the formula is correct. I created array of 4 for startTime n End Time. Plz guide me to solve this problem.

Private Sub Timer1_Timer()
Randomize

lblCar(n).Caption = Int(Rnd() * 50)

StartTime = Now
lblStart(n).Caption = Format(StartTime, "hh:mm:ss")

n = n + 1
If n = 4 Then
Timer1.Enabled = False
End If

End Sub

Private Sub Timer2_Timer()


EndTime = Now
lblEnd(i).Caption = Format(EndTime, "hh:mm:ss")
Timeduration = DateDiff("s", StartTime, EndTime)
lblDuration(i).Caption = "" & Timeduration

i = i + 1
If i = 4 Then
Timer2.Enabled = False
End If

End Sub
see these statements:
startTime = Now
endTime = Now

Each time Timers fire event, the startTime and andTime varriables always get the lastest moment.
Now, two timers have inteval is 5S different. If Timer2 fires event first, so Duration = Now - StartTime = 5
If two timer fire event together or Timer1 fires event first, so Duration = endTime-StartTime = Now - Now = 0.

I think, you should set the startTime and EndTime out of Timer1 (or Timer2)_Timer()
Mar 13 '08 #7
see these statements:
startTime = Now
endTime = Now

Each time Timers fire event, the startTime and andTime varriables always get the lastest moment.
Now, two timers have inteval is 5S different. If Timer2 fires event first, so Duration = Now - StartTime = 5
If two timer fire event together or Timer1 fires event first, so Duration = endTime-StartTime = Now - Now = 0.

I think, you should set the startTime and EndTime out of Timer1 (or Timer2)_Timer()
Thanks 4 the reply. i tryed ur suggestion but its x working. But i got question here . I need to store random in array of 10.Then i have to check if the random number exits or x if no i have to store number which been generatedin array of 10 (in any index ) at the same time i store time in at array time. How do i go abt.


Dim n As Integer



Private Sub Form_Load()

Randomize

For n = 0 To 9

lblCar(n).Caption = Int(Rnd() * 50)

Next n

End Sub

i dont know how to further plz guide me.
Mar 13 '08 #8
pureenhanoi
175 100+
Thanks 4 the reply. i tryed ur suggestion but its x working. But i got question here . I need to store random in array of 10.Then i have to check if the random number exits or x if no i have to store number which been generatedin array of 10 (in any index ) at the same time i store time in at array time. How do i go abt.


Dim n As Integer



Private Sub Form_Load()

Randomize

For n = 0 To 9

lblCar(n).Caption = Int(Rnd() * 50)

Next n

End Sub

i dont know how to further plz guide me.
If you store 10 random number by a For routine, the time for each number are the same (coz the coputer can store thousands of number in a Second). So, you can use a Timer like your first post

Desiging:

put one timer and one Command button into form
set Timer Interval to 1000 (or any inteval you want)

Coding:
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2. Dim numberStored As Integer
  3. Dim NumberArray(0 to 9) As Integer
  4. Dim TimeArray(0 to 9) As Date
  5.  
  6. Private Sub Command1_Click()
  7. numberStored = 0 'havent stored any number yet
  8. Timer1.Enabled = True   'start generate and storing
  9. Command1.Enabled = False 'prevent user click while timer running
  10. End Sub
  11.  
  12. Private Sub Timer1_Timer()
  13. Dim tmpNumber As Integer
  14. Dim i As Integer
  15. Timer1.Enabled = False    'stop timing while storing number
  16. Randomize
  17. tmpNumber = Int(Rnd() * 50)
  18. 'check if this number already exist
  19. For i = 0 To 9 
  20.  If NumberArray(i) = tmpNumber Then
  21.     'the number exist -> do not store it
  22.     Timer1.Enabled = True
  23.     Exit Sub
  24.  End If
  25. Next
  26. 'If this number does not exist -> store in Number Array
  27. Dim index As Integer
  28. index = AnyIndex    'calculate the index that you want
  29. NumberArray(index) = tmpNumber  'store the number
  30. TimeArray(index) = Now   'store the time
  31. numberStored = numberStored + 1
  32. If numberStored = 10 Then   'stop after stored 10 number
  33.  Timer1.Enabled = False
  34.  Command1.Enabled = True 'allow user use command to try again
  35. Else  'do not reach 10
  36.  Timer1.Enabled = True    'continue storing
  37. End If
  38. End Sub
Mar 13 '08 #9
Hi i try m modified ur code. ur code very helpful. I got some question here.

I need second timer. so i created one. To capture te endTime when car pass the second reader. The question here is can i randomize the time interval. this means i dont want to fix the time interval at properties window.

My code
Private Sub Timer2_Timer()
Randomize x
x = 2000 + Int(Rnd() * 3000)
Timer2.Interval = x
EndTime = Now
lblEnd(i).Caption = Format(EndTime, "hh:mm:ss")

End Sub

after i modifed ur code i add this code together. its not showing any output. Plz help me. give some comment abt my code.
Mar 14 '08 #10
Thanks for the reply. i understand what is the problem. but i still blur how to solve the problem. can i use randomize function for time interval. What i mean i dont want to fix the Timer 2 interval at the properties window but instead i want to randomize the interval. u might have clear of what i'm writing here by looking at my code


Private Sub Timer2_Timer()
Randomize x
x = 2000 + Int(Rnd() * 3000)
Timer2.Interval = x
EndTime = Now
lblEnd(i).Caption = Format(EndTime, "hh:mm:ss")

End Sub

This code didnt give any output. i need some suggestion here. guide me plz. can this be done?
Mar 14 '08 #11
harshadd
176 100+
Even this is too late to subscribe this thread now , I still wanted to know what exactly you were trying to achieve?
If you are trying to calculate time diff between two events then you do not need a timer at all.
Timers are basically used to repeat the same code after a fixed interval (in Milli seconds)
Mar 14 '08 #12
Even this is too late to subscribe this thread now , I still wanted to know what exactly you were trying to achieve?
If you are trying to calculate time diff between two events then you do not need a timer at all.
Timers are basically used to repeat the same code after a fixed interval (in Milli seconds)
Hi thanks for the reply, yes i'm trying to calculate time different between two event.
1. I have to create array of 10 (CarID)

2. I have to generate random number for 50.That generated random number is store in the array(CarID) that been created previously.

3. the generator random number have to be checked in array if its in array(CarID) or not. If not then store the random number in the array and store the Time In(means the car pass reader 1)
.
4. If the the random number/ counter in the Array then place current time out(means car passes at reader 2.After that calculate Time Duration and calculate Speed.

5. CarNum(random number), TimeIn, TimeOut, TimeDuration,Speed and Average Speed is dispay.

6. I have to reset this application after the it gets the Average Speed and calculate new average speed for new group of cars.



The problem i'm facing here i already did coding for the part 2. Where i have to generate random number and check but i have problem with the time in and time out. if u say i dont have to use timer then how i go about? plz guide me.
Mar 16 '08 #13
Killer42
8,435 Expert 8TB
Do you mean that you also need to generate random times?
Mar 17 '08 #14
harshadd
176 100+
Please tell me what I understood is correct or not?

I got it this way: you have 50 Cars (50 random Numbers)
when any number appeared 1st time say (no 7) that means ur car of that no 7 is started from reader1.(or say passed from reader1)
when the same number (ie no 7) appeared again (by random generator) that means ur car of no 7 is passed from reader 2.
Now here on pass 1 and pass2 you are keeping the time entry of each pass, and then you will calculate the difference between two time entries for car no 7 (in this example) and so on for all the 50 cars...

In short you are trying to get the time difference between same random nos generated twice?????
HND
Mar 18 '08 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: tym | last post by:
HELP!!! I'm going round the twist with this... I have a VB6 application which is using DAO to access a database (Please - no lectures on ADO, I know what I'm doing with DAO!!) Ok, problem...
6
by: Antti Laakso | last post by:
Hi i have function like above Public Sub halytystutkinta() Dim ds As New DataSet ds = dl2.HaeHalytys() Dim onkohal As Int16 onkohal = ds.Tables(0).Rows(0).Item("onkohalytys") halid =...
3
by: Kris Palmer | last post by:
hi, can somebody explain this problem? it's driving me crazy! i have a requirement to dynamically create a variable quantity of timers with associated start button based on the contents of a...
8
by: Stephen Rice | last post by:
Hi, I have a periodic problem which I am having a real time trying to sort. Background: An MDI VB app with a DB on SQL 2000. I have wrapped all the DB access into an object which spawns a...
2
by: zamir.khan | last post by:
Hello all, New to the groups, sorry if this the wrong forum/etiquette. I am coding a c++ application that requires the use of a timer-triggered event handler. I decided to use the timer provided...
3
by: ken | last post by:
Hello, I can't figure out how to solve this problem. I modified the timer example given in the help section. It increments a count every 3 millisecond in order to simulate a tank being filled with...
7
by: Fernando Barsoba | last post by:
Hi, After following the advice received in this list, I have isolated the memory leak problem I am having. I am also using MEMWATCH and I think it is working properly. The program does some...
2
by: r norman | last post by:
Please excuse the cross-posting. This question was raised in microsoft.public.dotnet.general but hasn't been answered so I am trying where I can. There are two of us who have the same problem...
6
by: Dave | last post by:
I have a service that has 6 different threads. Each thread has a timer on it that elapses at about the same time (if not the same time). When the timer elapses I am trying to log a message by...
2
by: Johnny Jörgensen | last post by:
I've got a process I want to run in a thread separate from my main application thread, so I've used a backgroundworker component, and in frmMain.Load I invoke the code using...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.