Convert timer interval to float - c#

Is it possible to make timer interval to float in c#.
If yes then how?

The timer interval is a double
Type: System.Double
The time, in milliseconds, between Elapsed events. The value must be greater than zero, and less than or equal to Int32.MaxValue. The default is 100 milliseconds.
http://msdn.microsoft.com/en-us/library/system.timers.timer.interval.aspx

Related

How to use NMEA time as elapsed time in c#?

In my windows form application I use stopwatch to calculate elapsed time between events but I want to get time from NMEA(GPRMC sentence) and want to use it as elapsed time. How can I do this?
Here is my code;
Stopwatch watch=new Stopwatch();
Textbox1.Text=gsaat.Substring(4,2);
//gsaat is the UTC time part of the GPRMC;
private void timer2_Tick(object sender, EventArgs e)
{
if ((Convert.ToDouble(textBox2.Text) >= Convert.ToDouble(label1.Text) && Convert.ToDouble(label1.Text) >= Convert.ToDouble(textBox1.Text)))
{
watch.Start();
var time = watch.Elapsed.TotalSeconds;
//I want to use gsaat as elapsed time in here instead of stopwatch
label2.Text = String.Format("{0:00.00}", time);
}
}
EDIT;
In my application I am making a acceleration test by using GNSS module. In application I am setting a boundary conditions for velocity Textbox1 and Textbox2 and label1 represents current velocity of the car and label2 represents elapsed time when car's current velocity enter these boundaries .For example; I set Textbox1.text=10 and Textbox2.text=100 and car started to accelerate and I want to measure time difference between car is accclerating from 10 to 100. To do this I used stopwatch but I faced some problems. Now I want to use UTC time to measure time between car's velocity is 10 and 100. I can get UTC time from sentences of the GPRMC and I parsed it too.However I can't make start it from zero when car's velocity is 10 and stopped it when car velocity passed 100.
Not really clear what you want to achieve, but a guess:
Parse the NMEA time and save it as DateTime object. Then do
var timeSpan = DateTime.UtcNow - nmeaTimestamp;
to get the time since the transmitted timestamp.
Have a look at NMEA documentation for the meaning of this timestamp: UTC of position fix: It is the time when the transmitting station was close to the transmitted position.
If you want to be alarmed after a particular timespan use a Timer. It will raise an event when the time has elapsed.
EDIT: Pick a packet close to velocity 10 and a packet close to velocity 100, parse the timestamps and subtract them:
var accelerationTimeMilliseconds = (timestampAt100 - timestampAt10).ElapsedMilliseconds;

Verify if a value is a tick in c#

I have a time tick (635655080937662522) I created with C# and stored in a database column. How can I verify that this value is a tick and not something else? Does a tick have a certain format?
As others have said, a tick is just a number, but there are some important differences, depending on where the tick came from.
Ticks from a DateTime or DateTimeOffset are 100ns each, and range from DateTime.MinValue.Ticks to DateTime.MaxValue.Ticks, which is 0 to 3155378975999999999. Note that this is much smaller than Int64.MaxValue.
Ticks from a TimeSpan are also 100ns each, but range from TimeSpan.MinValue.Ticks to TimeSpan.MaxValue.Ticks, which is the same as the range of Int64, which is -9223372036854775808 to 9223372036854775807.
Ticks from a Stopwatch (in the System.Diagnostics namespace) are of varying size, depending on the hardware capabilities of the system. They are usually around 4ns on modern hardware. You can adjust them to TimeSpan ticks by using Stopwatch.Elapsed.Ticks instead of Stopwatch.ElaspedTicks, or by taking the Stopwatch.Frequency into account in your calculations. The range is 0 to Int64.MaxValue.
Tick is a number, there is really not much more validation you can do.
You can check if actual time is in particular range that makes sense for your case, but it is application specific.

What is the default time precision of DateTime?

I did a small winform program for data transferring in Visual Studio, and I used a method to provide the transferring time duration. After the transferring being done, the program will return a dialog window to show the time.
But here I don't know what is the time precision or the resolution of the timer, how can it be such a precision, even micro second?
var startTime = DateTime.Now;
this.transferdata();
var endTime = DateTime.Now;
var timeElapsed = endTime.Subtract(startTime);
when I saw the definition of class DateTime, there is only a precision in milisecond. can anybody tell me why there is such a high resolution timer in the visual studio 2012? Or there is related to the operating system?
The precision of the clock depends on the operating system. The system clock ticks a certain number of times per second, and you can only measure whole ticks.
You can test the resolution for a specific computer using code like this:
DateTime t1 = DateTime.Now;
DateTime t2;
while ((t2 = DateTime.Now) == t1) ;
Console.WriteLine(t2 - t1);
On my computer the result is 00:00:00.0156253, which means that the system clock ticks 64 times per second.
(Note: The DateTime type also has ticks, but that is not the same as the system clock ticks. A DateTime tick is 1/10000000 second.)
To measure time more precisely, you should use the Stopwatch class. Its resolution also depends on the system, but is much higher than the system clock. You can get the resolution from the Stopwatch.Frequency property, and on my computer it returns 2143566 which is a tad more than 64...
Start a stopwatch before the work and stop it after, then get the elapsed time:
Stopwatch time = Stopwatch.StartNew();
this.transferdata();
time.Stop();
TimeSpan timeElapsed = time.Elapsed;
That will return the time in the resolution that the TimeSpan type can handle, e.g. 1/10000000 second. You can also calculate the time from the number of ticks:
double timeElapsed = (double)s.ElapsedTicks / (double)Stopwatch.Frequency;
You are confusing several things. Precision, Accuracy, Frequency, and Resolution.
You might have a variable that is accurate to a billion decimal places. But if you can't actually measure that small of a number then that's the difference between precision and resolution. Frequency is the number of times per second a measurement is taken, while relates to resolution. Accuracy is how closely a given sample is to the real measurement.
So, given that DateTime has a precision much higher than the system clock, simply saying DateTime.Now will not necessarily give you an exact timestamp. There are, however, Higher resolution timers in Windows, and the Stopwatch class uses them to measure time elapsed, so if you use this class you get a much better accuracy.
DateTime has no "default precision". It has only one precision, and that's the Minimum and Maximum values it can store. DateTime internally stores it's values as a single value, and this value is formatted to whatever type you want to display (seconds, minutes, days, ticks, whatever...).

Calculating a timeout that is suspended during certain periods

I've been tasked with writing a method that calculates, given a start datetime and timeout in minutes, the datetime that the timeout should occur.
However the timeout should only 'tick' during certain times on certain days, which is determined by a non tick start datetime, non tick stop datetime and a list of open days.
For example, given the following:
Start datetime: Friday 21/02/2014 15:00
Timeout: 720 minutes (12 hours)
Open days: Monday,Tuesday,Wednesday,Thursday,Friday
Non tick start time: 09:00
Non tick stop time: 18:30
The calculated timeout datetime should be Monday 24/02/2014 17:30 (as it does not tick on Saturday and Sunday).
I'm writing this in C#, but its technically language agnostic.
Is there an 'elegant' way of doing this? Or is it simply a case of looping and adding additional time where its determined that timeout should stop ticking?
If you set up a timer to tick every period (e.g. every second), you can increment a number of ticks in the timer's callback handler.
I would approach in one of the following ways. Either I could run the callback every second no matter what, and in the callback I could check whether I'm suppose to increment or skip the increment for this period (inside the non-tick time period).
Otherwise, I could increment every time no matter what, and then have a check to see if the non-tick period is approaching and, if so, then adjust the timer's callback time to run the next callback after the non-tick time period has elapsed.

Difference between ElapsedTicks, ElapsedMilliseconds, Elapsed.Milliseconds and Elapsed.TotalMilliseconds? (C#)

I'm totally confused between these 4. What is the difference between ElapsedMilliseconds (long), ElapsedTicks (long), Elapsed.TotalMilliseconds (double) and Elapsed.Milliseconds (int)?
I have a function
{
Stopwatch sw = new Stopwatch();
sw.Start();
MyTimeConsumingAction();
sw.Stop();
sw.//what?
}
How do I get the correct time consumed by my long running process from elapsed property of Stopwatch object in milliseconds?
Edit: I tried msdn documentation but it isn't anything detailed there..
Elapsed.TotalMilliseconds (double) returns the total number of whole and fractional milliseconds elapsed since inception
e.g. a stopwatch stopped at 1.23456 seconds would return 1234.56 in this property. See TimeSpan.TotalMilliseconds on MSDN
Elapsed.Milliseconds (int) returns the number of whole milliseconds in the current second
e.g. a stopwatch at 1.234 seconds would return 234 in this property. See TimeSpan.Milliseconds
ElapsedTicks (long) returns the ticks since start of the stopwatch.
In the context of the original question, pertaining to the Stopwatch class, ElapsedTicks is the number of ticks elapsed. Ticks occur at the rate of Stopwatch.Frequency, so, to compute seconds elapsed, calculate: numSeconds = stopwatch.ElapsedTicks / Stopwatch.Frequency.
The old answer defined ticks as the number of 100 nanosecond periods, which is correct in the context of the DateTime class, but not correct in the context of the Stopwatch class. See Stopwatch.ElapsedTicks on MSDN.
ElapsedMilliseconds returns a rounded number to the nearest full millisecond, so this might lack precision Elapsed.TotalMilliseconds property can give.
Elapsed.TotalMilliseconds is a double that can return execution times to the partial millisecond while ElapsedMilliseconds is Int64. e.g. a stopwatch at 0.0007 milliseconds would return 0, or 1234.56 milliseconds would return 1234 in this property. So for precision always use Elapsed.TotalMilliseconds.
See Stopwatch.ElapsedMilliseconds on MSDN for clarification.
Reflecting the Stopwatch class reveals that ElapsedMilliseconds is Elapsed ticks converted (and rounded) to milliseconds:
public TimeSpan Elapsed
{
get
{
return new TimeSpan(this.GetElapsedDateTimeTicks());
}
}
public long ElapsedMilliseconds
{
get
{
return this.GetElapsedDateTimeTicks() / 10000L;
}
}
in a short explanation from msdn:
ElapsedMilliseconds
This property represents elapsed time rounded down to the nearest whole millisecond value. For higher precision measurements, use the Elapsed or ElapsedTicks properties.
ElapsedTicks
This property represents the number of elapsed ticks in the underlying timer mechanism. A tick is the smallest unit of time that the Stopwatch timer can measure. Use the Frequency field to convert the ElapsedTicks value into a number of seconds.
Elapsed
Use the Elapsed property to retrieve the elapsed time value using TimeSpan methods and properties. For example, you can format the returned TimeSpan instance into a text representation, or pass it to another class that requires a TimeSpan parameter.
Elapsed is TimeSpan. If you want to display time, then just Elapsed.ToString() should do that

Categories

Resources