Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4272840
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T07:37:54+00:00 2026-05-21T07:37:54+00:00

I have some DateTime variable, and I want to use System.Threading.Timer to wait until

  • 0

I have some DateTime variable, and I want to use System.Threading.Timer to wait until this time arrive. If time is in the past I want the timer to tick immediately.

The problem is TimeSpan.TotalMilliseconds is double and timer due time biggest type islong.

I’ve tried to max the due-time to long.MaxValue using this code:

DateTime someUtcTime;
// Max due time to long.MaxValue
double doubleDueTime = Math.Min(
    (double)long.MaxValue,
    someUtcTime.Subtract(DateTime.UtcNow).TotalMilliseconds);

// Avoid negative numbers
long dueTime = Math.Max(0L, (long)doubleDueTime);

_timer.Change(dueTime, System.Threading.Timeout.Infinite);

but it turns out that casting long.MaxValue to double and back to long result a negative number (in unchecked code of curse). plz send me teh codez.


Edit: apparently, no matter which of Timer.Change overload you use, they are all limited to 4294967294 (UInt32.MaxValue - 1) milliseconds.


Solution:

cover both extreme cases (someUtcTime = DateTime.MaxValue; UtcNow = DateTime.MinValue; and vice versa).

const uint MAX_SUPPORTED_TIMEOUT = uint.MaxValue - 1; //0xfffffffe

DateTime someUtcTime;
double doubleDueTime = (someUtcTime - DateTime.UtcNow).TotalMilliseconds;

// Avoid negative numbers
doubleDueTime = Math.Max(0d, doubleDueTime);

// Max due time to uint.MaxValue - 1
uint dueTime = (uint)Math.Min(MAX_SUPPORTED_TIMEOUT, doubleDueTime);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T07:37:55+00:00Added an answer on May 21, 2026 at 7:37 am

    Since (DateTime.MaxValue - DateTime.MinValue).TotalMilliseconds is 315537897600000 and long.MaxValue is 9223372036854775807 (long can represent a value 4 orders of magnitude larger than the largest possible number of milliseconds between any two DateTime values), you can never have a time too far in the future.

    This will suffice:

    DateTime someUtcTime;
    // Max due time to long.MaxValue
    double doubleDueTime = (someUtcTime - DateTime.UtcNow).TotalMilliseconds;
    
    // Avoid negative numbers
    long dueTime = Math.Max(0L, (long)doubleDueTime);
    
    _timer.Change(dueTime, System.Threading.Timeout.Infinite);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have datetime objects in UTC time and I want to convert them
I have a numerous small pieces of data that I want to be able
I have a web app built in coldfusion that receives medical referral letters from
I have an application which sends a POST request to the VB forum software
I am building an advanced image sharing web application. As you may expect, users
After reading on stackoverflow that in the case of checking the format of a
I am trying to build a lambda expression, containing two assignments (as shown further
I am creating several directories a day. After seven days I am going to
Is there a way, in PHP5.3, for an object $enclosed to know which object
I think I need a way to perform a pivot or crosstab using C#

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.