I'm working with Milisecond and I've used it like
Timeout = (int)TimeSpan.FromMinutes(TimeoutVal).TotalMilliseconds
But I read on few places people are casting it to long
instead of int
? Why is that?
I'm working with Milisecond and I've used it like
Timeout = (int)TimeSpan.FromMinutes(TimeoutVal).TotalMilliseconds
But I read on few places people are casting it to long
instead of int
? Why is that?
The only reason why I'd cast is if it's using an API that only allows an Int to be in timeout (e.g Thread.Sleep or Task.Delay), but in general rule you shouldn't, unless a) you don't mind if it overflows and cause "unexpected" behaviour or b) if it shouldn't overflow (like, you're using a small timeout).
In all other purposes, do not cast. Store it in double instead.
To calculate the max. number of hours in milliseconds you can store in a signed int32:
1s = 1000ms
1m = 60s
1h = 60m
Int32 max value: 2147483647
Then:
int totalSeconds = int.MaxValue / 1000;
int totalMinutes = totalSeconds / 60;
int totalHours = totalMinutes / 60;
The result would be 596.5232 hours.
Anyway, you shouldn't use a Timeout with such high values, instead, use a cancellation token. But again, it depends on the API you are working on.