开发者

Quartz .Net Job should reschedule itself

I'm using a Quartz .NET job to get data from serial port periodically. The problem is that if the serial port is used by a another programm the job should reschedule itself to maybe try it in 30min again. How can I accomplish that?

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)
        {
            ReadOutHel开发者_Go百科perClass.ShowNotifiy(name, mynotifyicon);
        }

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

    }


I have tried something. This add an new job which is an copy of the old just delayed. Maybe it's better to add another trigger to existing job, but i don't get it to workd with an second trigger.

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;

    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜