为什么我的cancelestTokenSource抛出空对象异常?

时间:2019-05-16 09:19:50

标签: c# arrays scheduling cancellationtokensource

我有一段代码计划了一系列方法,我将其重构以更好地控制停止和启动所述方法。

我遇到的问题是,在重构的代码中,我得到了'Null 取消令牌上的“引用异常”,我不理解,因为我以几乎完全相同的方式声明了它,仅在数组中声明了它。

原始代码:

public partial class Form1 : Form
{       
    int startTime1 = 0, startTime2 = 6, startTime3 = 12, startTime4 = 18;
    int startMin1 = 0, startMin2 = 0, startMin3 = 0, startMin4 = 0;        

    CancellationTokenSource ct1, ct2, ct3, ct4;
    Manager Launcher = new Manager();

    private void Form1_Load(object sender, EventArgs e)
    {}

    public Form1()
    {InitializeComponent();}  

  // AND A BUNCH OF METHODS

}

在此类中,我将取消令牌与

一起使用
public void RunCodeAt(int Number,  DateTime date)
    {
        switch (Number)
        {
            case 1:
                ct1 = new CancellationTokenSource();
                break;
            case 2:
                ct2 = new CancellationTokenSource();
                break;
            case 3:
                ct3 = new CancellationTokenSource();
                break;
            case 4:
                ct4 = new CancellationTokenSource();
                break;
        }
        var dateNow = DateTime.Now;
        TimeSpan ts;
        if (date > dateNow)
        {
            ts = date - dateNow;
        }
        else
        {
            date = GetNextDate(date);
            ts = date - dateNow;
        }



        switch (Number)
        {
            case 1:
                Task.Delay(ts).ContinueWith((x0) =>
                {
                    methodToCall(date,Number);
                    RunCodeAt(Number, GetNextDate(date)); // Schedule next run.
                }, ct1.Token);
                break;
            case 2:
                Task.Delay(ts).ContinueWith((x0) =>
                {
                    methodToCall(date, Number);
                    RunCodeAt(Number, GetNextDate(date)); // Schedule next run.
                }, ct2.Token);
                break;
            case 3:
                Task.Delay(ts).ContinueWith((x0) =>
                {
                    methodToCall(date, Number);
                    RunCodeAt(Number, GetNextDate(date)); // Schedule next run.
                }, ct3.Token);
                break;
            case 4:
                Task.Delay(ts).ContinueWith((x0) =>
                {
                    methodToCall(date, Number);
                    RunCodeAt(Number, GetNextDate(date)); // Schedule next run.
                }, ct4.Token);
                break;
        }
    } // RunCodeAt()

那为什么当我尝试使用这样的数组来实现...


public partial class Form1 : Form
    {

            public int[] SH = { 12, 13, 14, 15, 16, 17, 18, 19 };
            public int[] EH = { 12, 13, 14, 15, 16, 17, 18, 19 };
            public int[] SM = { 00, 00, 00, 00, 00, 00, 00, 00 };
            public int[] EM = { 59, 59, 59, 59, 59, 59, 59, 59 };


        public CancellationTokenSource[] cts = new CancellationTokenSource[8];
        public CancellationTokenSource[] ctc = new CancellationTokenSource[8]; // Cancellation token start and close

确实

 public void BeginInstancAt(int Number, DateTime date)
        {
            cts[Number] = new CancellationTokenSource();
            try
            {                
                var dateNow = DateTime.Now;
                TimeSpan ts;
                if (date > dateNow)
                {
                    ts = date - dateNow;
                }
                else
                {
                    date = GetNextDate(date);
                    ts = date - dateNow;
                }

                Task.Delay(ts).ContinueWith((x0) =>
                {
                    startClientScheduled(date, Number);
                    BeginInstancAt(Number, GetNextDate(date)); // Schedule next run.
                }, cts[Number].Token);
            }
            catch (Exception cuntflaps)
            {
                MessageBox.Show("FATAL INITIATION SCHEDULING EXCEPTION");
                Console.WriteLine(cuntflaps.ToString());
            }


        }

导致

System.NullReferenceException: Object reference not set to an instance of an object.
   at SchedulerV1_0.Form1.BeginInstancAt(Int32 Number, DateTime date)
Exception thrown: 'System.NullReferenceException' in SchedulerV1_0.exe

被扔到begininstanceat的第一行吗?

如果我将鼠标悬停在beginInstanceAt()中的cts [number]上,它将显示字段cancelingtokensource [] Form1.cts,这表明它确实可以访问该字段。

我应该补充一点,通过使用switch语句并使用数组外部的标记,我已经能够使此代码起作用。

0 个答案:

没有答案