Timespan错误负最大int值错误 [英] Timespan error negative max int value error

查看:120
本文介绍了Timespan错误负最大int值错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好



Hello

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));

        }





当我在中午12点运行此代码时出现错误,即未处理 mscorlib.dll中出现'System.ArgumentOutOfRangeException'类型的异常



附加信息:该数字必须是非负数且小于或等于Int32.MaxValue,或者-1。



我尝试了什么:



当我将时间更改为上午9:00此错误未显示。



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.

推荐答案

当调度时间已经过去且时,这将为负数intervalPeriod 小于当前/ 2 (例如,当用 timeBetweenEachRun 一小时打电话时) :

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):
// 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:

double firstExecution = scheduledTime - current;
// If scheduling time already passed today, do it tomorrow
if (firstExecution <= 0)
    firstExecution += 24 * 60 * 60 * 1000;


这篇关于Timespan错误负最大int值错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆