Quartz .Net Job应该重新安排自己

时间:2011-09-20 07:50:03

标签: c# triggers serial-port scheduled-tasks quartz.net

我正在使用Quartz .NET作业定期从串口获取数据。 问题是如果串口被另一个程序使用,作业应该重新安排自己,以便再次尝试30分钟。我怎么能做到这一点?

public virtual void Execute(JobExecutionContext context)
    { 
        SetYModemEvents();
        JobDataMap data = context.JobDetail.JobDataMap;
        string COM = data.GetString("COM");
        string BAUD = data.GetString("BAUD");
        string name = data.GetString("NAME");

        _yModem.AllowDisconnect = true;

        _connection.Port.PortName = COM;
        try
        {
            _connection.Port.BaudRate = int.Parse(BAUD);
        }catch(FormatException)
        {
            _connection.Port.BaudRate = 9600;
        }
        try
        {
            _connection.Port.Open();
        }catch(UnauthorizedAccessException ex)
        {
            //Reschedule this job

        }

        _connection.Port.DataReceived += new SerialDataReceivedEventHandler(DataReceviedHandler);
        DeadManSwitch.Tick += new EventHandler(DeadManSwitch_Tick);
        DeadManSwitch.Start();

        if(Properties.Settings.Default.UseBubbels)
        {
            ReadOutHelperClass.ShowNotifiy(name, mynotifyicon);
        }

        //Starten der Auslesung
        _connection.Write(protocoll.ReadLoggerData(), 0, protocoll.ReadLoggerData().Length);
        DeadManSwitch.Interval = DeadManTimeOut;

    }

1 个答案:

答案 0 :(得分:0)

我尝试了一些东西。这增加了一个新工作,这是旧的刚刚延迟的副本。也许最好为现有作业添加另一个触发器,但我不会让它与第二个触发器一起工作。

public virtual void Execute(JobExecutionContext context)
    { 
        SetYModemEvents();
        JobDataMap data = context.JobDetail.JobDataMap;
        string COM = data.GetString("COM");
        string BAUD = data.GetString("BAUD");
        string name = data.GetString("NAME");

        _yModem.AllowDisconnect = true;

        _connection.Port.PortName = COM;
        try
        {
            _connection.Port.BaudRate = int.Parse(BAUD);
        }catch(FormatException)
        {
            _connection.Port.BaudRate = 9600;
        }
        try
        {
            _connection.Port.Open();
        }catch(UnauthorizedAccessException ex)
        {
            //TODO Testen
            Trigger[] trArray = context.Scheduler.GetTriggersOfJob(context.JobDetail.Name, context.JobDetail.Group);
            if (trArray.Length >= 2)
            {
                trArray.ToList().RemoveRange(1, 1);
            }
            JobDetail JB = context.JobDetail;
            Trigger delayTrigger = new SimpleTrigger(JB.Name + "_Delay", "DGroup", DateTime.UtcNow.AddMinutes(_delay), null, 1, TimeSpan.FromMinutes(_delay)); 
            JB.Name = JB.Name + "_Delay";
            JB.Group = "DGroup";

            context.Scheduler.ScheduleJob(context.JobDetail, delayTrigger);
            return;
        }


        _connection.Port.DataReceived += new SerialDataReceivedEventHandler(DataReceviedHandler);
        DeadManSwitch.Tick += new EventHandler(DeadManSwitch_Tick);
        DeadManSwitch.Start();

        if(Properties.Settings.Default.UseBubbels)
        {
            ReadOutHelperClass.ShowNotifiy(name, mynotifyicon);
        }

        //Starten der Auslesung
        _connection.Write(protocoll.ReadLoggerData(), 0, protocoll.ReadLoggerData().Length);
        DeadManSwitch.Interval = DeadManTimeOut;

    }