繁体   English   中英

类型为“ System.IndexOutOfRangeException”的未处理异常C#

[英]An unhandled exception of type 'System.IndexOutOfRangeException' C#

请帮助建立发布错误

    using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;

namespace Runtime
{
    class Program
    {
        static Timer timer;
        static string date = "";
        static string time = "";
        static void Main(string[] args)
        {
            date = args[0].ToString();


            time = args[1].ToString();
            schedule_Timer();
            Console.ReadLine();

        }

        static void schedule_Timer()
        {
            Console.WriteLine("### Timer Started ###");

            DateTime nowTime = DateTime.Now;
            DateTime scheduledTime = DateTime.ParseExact(date + " " + time, "yyyy-MM-dd HH:mm", new CultureInfo("en-US"));
            //if (nowTime > scheduledTime)
            //{
            //    return;
            //}


            double tickTime = (double)(scheduledTime - DateTime.Now).TotalMilliseconds;

            timer = new System.Timers.Timer(tickTime);
            timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
            timer.Enabled = true;
            timer.Start();

        }


        static void timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            Console.WriteLine("### Timer Stopped ### \n");
            timer.Stop();
            Console.WriteLine("### Scheduled Task Started ### \n\n");
            Console.WriteLine("Do - Performing scheduled task\n");
            Console.WriteLine("### Task Finished ### \n\n");
            //----------------------------------------------------------------


            //schedule_Timer();
        }
    }
}

显示错误

引发异常:Runtime.exe中的“ System.IndexOutOfRangeException”。Runtime.exe中发生了类型为“ System.IndexOutOfRangeException”的未处理异常。索引超出了数组的范围。

程序“ [7172] Runtime.exe”已退出,代码为-1(0xffffffff)。

第25行错误System.IndexOutOfRangeException:'索引在数组的边界之外。'

乍一看,我会说args[]不包含任何元素或仅包含1个元素。 您应该先添加支票,然后再访问它。

if (args == null)
{
    if (args.Length > 0) // or for your specific case: (args.Length >= 2)
        //do your stuff (including further appropriate checks)
}

编辑:更改//do whatever you like的以解决rmjoia的评论

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM