作业调度程序不适用于程序退出

时间:2015-08-27 18:40:34

标签: c# multithreading scheduled-tasks

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

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

enter image description here

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

0 个答案:

没有答案
相关问题