Click here to Skip to main content
15,908,776 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello

C#
private void timingToolStripMenuItem_Click(object sender, EventArgs e)
        {
            StartTimer(new TimeSpan(10, 00 , 0), new TimeSpan(24,00 , 0), ProcessViewType.SingleRun, false);
            StartTimerH(new TimeSpan(09, 30, 0), new TimeSpan(01, 00, 0), ProcessViewType.ReportToBranch, false);


        }

        protected void StartTimer(TimeSpan scheduledRunTime, TimeSpan timeBetweenEachRun,ProcessViewType pc,bool Hourly)
        {
            // Initialize timer
            double current = DateTime.Now.TimeOfDay.TotalMilliseconds;
            double scheduledTime = scheduledRunTime.TotalMilliseconds;
            double intervalPeriod = timeBetweenEachRun.TotalMilliseconds;
            // calculates the first execution of the method, either its today at the scheduled time or tomorrow (if scheduled time has already occured today)
            double firstExecution = current > scheduledTime ? intervalPeriod + (intervalPeriod - current) : scheduledTime - current;
            System.Threading.TimerCallback callback = null;
  callback = new System.Threading.TimerCallback(runSingleRun);

            _timer = new System.Threading.Timer(callback, null, Convert.ToInt32(firstExecution), Convert.ToInt32(intervalPeriod));

        }


when i run this code on 12:00 pm i got error which is " An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll

Additional information: The number must be non-negative and less than or equal to Int32.MaxValue, or -1."

What I have tried:

When i change the time to 9:00 am this error does not show.
Posted
Updated 9-Mar-17 23:29pm
Comments
Graeme_Grant 10-Mar-17 4:54am    
Do you understand debugging? Have you inspected the value of the variables on the line throwing the error? You will see exactly why.
CHill60 10-Mar-17 5:03am    
When you originally posted this question 4 days ago you were advised to use the debugger to determine what was going on.
We can't debug this for you - runSingleRun and ProcessViewType are unknown to us
Member 10525430 10-Mar-17 5:05am    
i found the the error on 4 days ago question.This is different error this is i cant run the pass time span it catch exception.
Graeme_Grant 10-Mar-17 5:25am    
As mentioned above, simple debugging 101, you will see your problem.

Basic Debugging with Visual Studio 2010 - YouTube[^]
Ralf Meier 10-Mar-17 5:14am    
have you tried to add 1 day to the value if it is negative (because you substract to higher value from a lower value from day-change) ?

1 solution

This will be negative when the scheduling time has already passed and the intervalPeriod is less than current / 2 (e.g. when calling with timeBetweenEachRun of one hour):
C#
// calculates the first execution of the method, either its today at the scheduled time or tomorrow (if scheduled time has already occured today)
double firstExecution = current > scheduledTime ? intervalPeriod + (intervalPeriod - current) : scheduledTime - current;

Use this instead:
C#
double firstExecution = scheduledTime - current;
// If scheduling time already passed today, do it tomorrow
if (firstExecution <= 0)
    firstExecution += 24 * 60 * 60 * 1000;
 
Share this answer
 
Comments
Member 10525430 11-Mar-17 6:51am    
Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900