C# 作业计划程序在程序退出时不工作

C# 作业计划程序在程序退出时不工作,c#,multithreading,scheduled-tasks,C#,Multithreading,Scheduled Tasks,我的代码有点草率,但我有这个问题。我开发了一个时间/日计划程序,但问题是当我关闭程序窗口,然后重新打开它时,它只对接下来的两个计划任务有效,而对第三个计划任务则不再有效 我的意思是,如果我选择每一分钟运行一次代码,时间是9:11,然后我将关闭窗口几秒钟,然后重新打开它,它将在9:12和9:13运行,但不会在9:14运行 这是一个巨大的代码后,希望我们阅读它。你花点时间拿出一个简短但完整的程序来演示这个问题,怎么样?提示:所需的UI是绝对最小的…我只是不知道问题出在哪里这就是我发布它的原因,所以你

我的代码有点草率,但我有这个问题。我开发了一个时间/日计划程序,但问题是当我关闭程序窗口,然后重新打开它时,它只对接下来的两个计划任务有效,而对第三个计划任务则不再有效

我的意思是,如果我选择每一分钟运行一次代码,时间是9:11,然后我将关闭窗口几秒钟,然后重新打开它,它将在9:12和9:13运行,但不会在9:14运行


这是一个巨大的代码后,希望我们阅读它。你花点时间拿出一个简短但完整的程序来演示这个问题,怎么样?提示:所需的UI是绝对最小的…我只是不知道问题出在哪里这就是我发布它的原因,所以你要花时间尝试将问题缩小到最小-复制一份,这样你就不会丢失真正的代码,然后开始删除所有不相关的东西,比如大部分UI。你所需要的只是计时器启动时显示的东西。。。您不需要任何用户输入。你不需要任何文件访问,除非我误解了。你只需要足够让我们看到问题。
public partial class MainForm : Form
{
    public MainForm()
    {
        IFormatter formatter = new BinaryFormatter();
        Stream stream = new FileStream("MyFile.bin", FileMode.Open, FileAccess.Read, FileShare.Read);
        Test t1 = (Test)formatter.Deserialize(stream);
        stream.Close();

        hourArray[0] = Convert.ToInt32(t1.hr);
        minutesArray[0] = Convert.ToInt32(t1.min);
        radioBoxArray[0] = Convert.ToBoolean(t1.radioBox1);
        radioBoxArray[1] = Convert.ToBoolean(t1.radioBox2);
        radioBoxArray[2] = Convert.ToBoolean(t1.radioBox3);
        radioBoxArray[3] = Convert.ToBoolean(t1.radioBox4);
        radioBoxArray[4] = Convert.ToBoolean(t1.radioBox5);
        radioBoxArray[5] = Convert.ToBoolean(t1.radioBox6);
        timeToCall = t1.StoreNextDate;

        InitializeComponent();


       button01_Click();
        // create reader & open file



        // TODO: Add constructor code after the InitializeComponent() call.
        //
    }//end constru

    CancellationTokenSource m_ctSource;


    private void button01_Click(object sender, EventArgs e)
    {

        hour = (int)numHours.Value;

        int minutes = (int)numMins.Value;

        //create next date which we need in order to run the code
        var dateNow = DateTime.Now;
        var date = new DateTime(dateNow.Year, dateNow.Month, dateNow.Day, hour, minutes, 0);

        var curDateValue = getCurrentDate(date, getScheduler());
        runCodeOnce(curDateValue, getScheduler());

        var nextDateValue = getNextDate(date, getScheduler());
        runCodeAt(nextDateValue, getScheduler());
        timeToCall = getNextDate(date, getScheduler());

        Test t2 = new Test();

        t2.hr = numHours.Value.ToString();
        t2.min = numMins.Value.ToString();
        t2.radioBox1 = radioButton1.Checked.ToString();
        t2.radioBox2 = radioButton2.Checked.ToString();
        t2.radioBox3 = radioButton3.Checked.ToString();
        t2.radioBox4 = radioButton4.Checked.ToString();
        t2.radioBox5 = radioButton5.Checked.ToString();
        t2.radioBox6 = radioButton6.Checked.ToString();
        t2.StoreNextDate = timeToCall;


        IFormatter formatter = new BinaryFormatter();
        Stream stream = new FileStream("MyFile.bin", FileMode.Create, FileAccess.Write, FileShare.None);
        formatter.Serialize(stream, t2);
        stream.Close();


    }




    private void button01_Click()
    {


        hour = (int)numHours.Value;

        int minutes = (int)numMins.Value;


           //create next date which we need in order to run the code
        var dateNow = DateTime.Now;
        var date = new DateTime(dateNow.Year, dateNow.Month, dateNow.Day, hour, minutes, 0);


        var curDateValue = getCurrentDate(date, getScheduler());
        runCodeOnce(curDateValue, getScheduler());


        var nextDateValue = getNextDate(date, getScheduler());
        runCodeAt(nextDateValue, getScheduler());
        timeToCall = getNextDate(date, getScheduler());





        Test t2 = new Test();

        t2.hr = numHours.Value.ToString();
        t2.min = numMins.Value.ToString();
        t2.radioBox1 = radioButton1.Checked.ToString();
        t2.radioBox2 = radioButton2.Checked.ToString();
        t2.radioBox3 = radioButton3.Checked.ToString();
        t2.radioBox4 = radioButton4.Checked.ToString();
        t2.radioBox5 = radioButton5.Checked.ToString();
        t2.radioBox6 = radioButton6.Checked.ToString();
        t2.StoreNextDate = timeToCall;




        IFormatter formatter = new BinaryFormatter();
        Stream stream = new FileStream("MyFile.bin", FileMode.Create, FileAccess.Write, FileShare.None);
        formatter.Serialize(stream, t2);
        stream.Close();



    }

 //   CancellationTokenSource m_ctSource;





    private void runCodeAt(DateTime date, Scheduler scheduler)
    {

        m_ctSource = new CancellationTokenSource();

        var dateNow = DateTime.Now;
        TimeSpan ts;


        if (date > dateNow)
            ts = date - dateNow;

        else
        {
            date = getNextDate(date, scheduler);
            ts = date - dateNow;
        }

        //enable the progressbar
        prepareControlForStart();

            try
            {
                TaskEx.Delay(ts).ContinueWith((x) =>
                {
                    //run the code at the time
                    methodToCall(date);
                    runCodeAt(getNextDate(date, scheduler), scheduler);
                }
                  , m_ctSource.Token);
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: " + e.ToString());
            }
    }

    private void methodToCall(DateTime time)
    {
        System.Diagnostics.Process.Start("cmd.exe", @"/c C:\Doit.bat");

        //setup next call
        var nextTimeToCall = getNextDate(time, getScheduler());
        timeToCall = nextTimeToCall;
        this.BeginInvoke((Action)(() =>
        {

                   Test t31 = new Test();

        t31.StoreNextDate = timeToCall;

        t31.hr = numHours.Value.ToString();
        t31.min = numMins.Value.ToString();
        t31.radioBox1 = radioButton1.Checked.ToString();
        t31.radioBox2 = radioButton2.Checked.ToString();
        t31.radioBox3 = radioButton3.Checked.ToString();
        t31.radioBox4 = radioButton4.Checked.ToString();
        t31.radioBox5 = radioButton5.Checked.ToString();
        t31.radioBox6 = radioButton6.Checked.ToString();
        t31.StoreNextDate = timeToCall;

        IFormatter formatter31 = new BinaryFormatter();
        Stream stream31 = new FileStream("MyFile.bin", FileMode.Create, FileAccess.Write, FileShare.None);
        formatter31.Serialize(stream31, t31);
        stream31.Close(); 

        //MessageBox.Show(strText);
        }));

        //  System.Diagnostics.Process.Start("cmd.exe", @"/c C:\Doit.bat");
    }
}