6 Replies - 3180 Views - Last Post: 19 November 2009 - 01:24 PM Rate Topic: -----

#1 sharpy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 36
  • View blog
  • Posts: 394
  • Joined: 02-June 07

tick Timer Problem

Post icon  Posted 17 November 2009 - 01:29 PM

Hi all

What I am trying to do is set a timer going which is set off by an output from an external signal from a machine tool.
At present the code is activated through a button on my form.

This is the code I am using


Public Class Form1
	Private CompTime As System.Int32

	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		CompTime = Environment.TickCount
	End Sub

	Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
		Label2.Text = TimeOfDay
	End Sub

	Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick

		Dim CurrentTickValue As System.Int32 = Environment.TickCount
		Dim Difference As System.Int32 = CurrentTickValue - CompTime
		Dim Hours As System.Int32
		Dim Minutes As System.Int32
		Dim Seconds As System.Int32

		Hours = (Difference / (3600 * 1000)) Mod 24
		Minutes = (Difference / (60 * 1000)) Mod 60
		Seconds = (Difference / 1000) Mod 60

		Label1.Text = String.Format("This application has been running for " _
		& Hours & " Hours " & Minutes & " Minutes" & Seconds & " Seconds")

	End Sub

	Private Sub TimerStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerStart.Click
		Timer2.Enabled = True
	End Sub

	Private Sub Timerstop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timerstop.Click
		Timer2.Enabled = False
	End Sub

End Class



The problem I am having is when the timer reaches 30 seconds a minute is added. Then after a further 60 seconds another minute is added. At first I thought the minutes were being rounded up but if I change the minutes calulation

from this

Minutes = (Difference / (60 * 1000)) Mod 60


to this

Minutes = (Difference / (120 * 1000)) Mod 60


Then it will add a minute after 60 seconds have gone by which is correct.

I'm sure this is something simple but I can not see what the problem is. Logic says the calculation is correct but obviously not. I have not tried the hours yet will cross that bridge when I get to it.

Hope you can help

regards

Sharpy

Is This A Good Question/Topic? 1
  • +

Replies To: tick Timer Problem

#2 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: tick Timer Problem

Posted 17 November 2009 - 02:57 PM

I would suggest using TimeSpan for your conversions as it can easily convert from any type of time to any other type of time. You can use the following to convert your Difference variable to Days, Hours, Minutes and Seconds.

Days = TimeSpan.FromMilliseconds(Difference).TotalDays
Hours = TimeSpan.FromMilliseconds(Difference).TotalHours
Minutes = TimeSpan.FromMillisecond(Diffrence).TotalMinutes
Seconds = TimeSpan.FromMilliseconds(Difference).TotalSeconds



Now, you are going to run into a problem here. When you do each of the conversions you are going to want to deduct the value of each of those from the elapsed time. For example, if you have a certain number of days when you try and calculate the hours the number of hours will include the number of days unless you removed the number of days. That is why you are having problems with your calculations. There is an easy fix to this though. Doing the following will solve that problem.

Dim TotalMilliseconds As System.Int32

TotalMilliseconds = Difference
Days = TimeSpan.FromMilliseconds(TotalMilliseconds).TotalDays
TotalMilliseconds -= TimeSpan.FromDays(Days).TotalMilliseconds

Hours = TimeSpan.FromMilliseconds(TotalMilliseconds).TotalHours
TotalMilliseconds -= TimeSpan.FromHours(Hours).TotalMilliseconds

Minutes = TimeSpan.FromMilliseconds(TotalMilliseconds).TotalMinutes
TotalMilliseconds -= TimeSpan.FromMinutes(Minutes).TotalMilliseconds

Seconds = TimeSpan.FromMilliseconds(TotalMilliseoncdds).TotalSeconds


Was This Post Helpful? 1
  • +
  • -

#3 sharpy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 36
  • View blog
  • Posts: 394
  • Joined: 02-June 07

Re: tick Timer Problem

Posted 18 November 2009 - 10:15 AM

Hi Sixofeleven

Thanks for that will have a play and get back to you.

Best regards

Sharpy
Was This Post Helpful? 0
  • +
  • -

#4 sharpy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 36
  • View blog
  • Posts: 394
  • Joined: 02-June 07

Re: tick Timer Problem

Posted 18 November 2009 - 11:00 AM

Hi Sixofeleven

Firstly thanks for your time. I tried what you suggested which unfortunately resulted in a similar problem.

Once the seconds hits 30 then a minute is added but the seconds then changes to -30 then continues to count up to 0 and beyond, which is correct as the code is saying take away 60 from 30 then start again. I think the problem lies somewhere else within the code but i am not sure where. Its treating 30 seconds as 60 then incrementing the minutes.

Public Class Form1
	Private CompTime As System.Int32
	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

	End Sub
	Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

		Dim days As System.Int32
		Dim Hours As System.Int32
		Dim Minutes As System.Int32
		Dim Seconds As System.Int32

		Dim TotalMilliseconds As System.Int32
		Dim CurrentTickValue As System.Int32 = Environment.TickCount
		Dim Difference As System.Int32 = CurrentTickValue - CompTime

		TotalMilliseconds = Difference

		days = TimeSpan.FromMilliseconds(TotalMilliseconds).TotalDays
		TotalMilliseconds -= TimeSpan.FromDays(days).TotalMilliseconds

		Hours = TimeSpan.FromMilliseconds(TotalMilliseconds).TotalHours
		TotalMilliseconds -= TimeSpan.FromHours(Hours).TotalMilliseconds

		Minutes = TimeSpan.FromMilliseconds(TotalMilliseconds).TotalMinutes
		TotalMilliseconds -= TimeSpan.FromMinutes(Minutes).TotalMilliseconds

		Seconds = TimeSpan.FromMilliseconds(TotalMilliseconds).TotalSeconds

		Label1.Text = (" days " & days & " Hours " & Hours & " Minutes " _
					   & Minutes & " Seconds " & Seconds)
	End Sub
	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

		CompTime = Environment.TickCount
		Timer1.Enabled = True

	End Sub

	Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
		Timer1.Enabled = False
	End Sub
End Class



I have posted the full code so you can copy and paste into a form.

I am using 2 buttons a timer and a label. The timer interval is set to 1

Hope you can help

Sharpy

This post has been edited by sharpy: 18 November 2009 - 11:15 AM

Was This Post Helpful? 0
  • +
  • -

#5 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: tick Timer Problem

Posted 18 November 2009 - 06:43 PM

Sorry, the error was mine. I did find a solution to your problem though and it will give you better resolution than using Environment.TickCount. The StopWatch class is much better for this problem as it is a high resolution timer than Environment.TickCount which is not really all that accurate. I believe part of the problem was you were having days, hours, minutes and seconds inside the tick event. I moved them outside and I've tested the code for a full minute and everything worked fine. Basically what you want to do is create an instance of the StopWatch class. Then in the click event of the button, or when you receive a signal, you can reset the stop watch to 0, start the stop watch, set the interval of the timer and start the timer. Then in the stop event you just stop the stop watch and stop the timer. I also reworked the code for converting the stop watch's output, in milliseconds, to get the number of seconds, minutes, hours and days. The way it works is you first get the number of seconds by using Mod 60. Then, if the seconds is greater or equal to 60 you know you have minutes so you can safely calculate the minutes. I calculated the minutes by taking the total seconds dividing it by 60 and then using mod 60. Something similar for hours and days. This is the complete code for the form. It had 1 label, two buttons and a timer on it.

Option Explicit On

Public Class Form1

	Dim stopWatch As Stopwatch = New Stopwatch()
	Dim days As System.Int64
	Dim Hours As System.Int64
	Dim Minutes As System.Int64
	Dim Seconds As System.Int64

	Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
		Dim TotalSeconds As System.Int64

		TotalSeconds = TimeSpan.FromMilliseconds(stopWatch.ElapsedMilliseconds _
												 ).TotalSeconds

		Seconds = TotalSeconds Mod 60

		If (TotalSeconds >= 60) Then
			Minutes = (TotalSeconds / 60) Mod 60
		End If

		If (TotalSeconds >= 60 * 60) Then
			Hours = (TotalSeconds / (60 * 60) Mod 24)
		End If

		If (TotalSeconds >= 24 * 60 * 60) Then
			days = (TotalSeconds / (24 * 60 * 60) Mod 365)
		End If

		Label1.Text = (" days " & days & " Hours " & Hours & " Minutes " _
					   & Minutes & " Seconds " & Seconds)
	End Sub

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		stopWatch.Reset()
		stopWatch.Start()
		Timer1.Interval = 1000
		Timer1.Start()
	End Sub

	Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
		stopWatch.Stop()
		Timer1.Stop()
	End Sub
End Class


Was This Post Helpful? 1
  • +
  • -

#6 sharpy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 36
  • View blog
  • Posts: 394
  • Joined: 02-June 07

Re: tick Timer Problem

Posted 18 November 2009 - 11:47 PM

Hi Sixofeleven

Thanks for that I am just starting work now so I will have a play later when I get home. The code looks good.

Cheers
Was This Post Helpful? 0
  • +
  • -

#7 sharpy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 36
  • View blog
  • Posts: 394
  • Joined: 02-June 07

Re: tick Timer Problem

Posted 19 November 2009 - 01:24 PM

Hi Sixofeleven


The code worked perfectly. Thank you for your time and patience. I see from your tutorials you work with timers a lot so you were the obvious choice for this problem.

best regards

Sharpy :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1