mirror of
https://github.com/kevincolyar/CronNET.git
synced 2025-03-24 05:17:44 +00:00
48 lines
1.1 KiB
C#
48 lines
1.1 KiB
C#
using System;
|
|
using System.Threading;
|
|
|
|
namespace CronNET
|
|
{
|
|
public interface ICronJob
|
|
{
|
|
void execute(DateTime date_time);
|
|
void abort();
|
|
}
|
|
|
|
public class CronJob : ICronJob
|
|
{
|
|
private readonly ICronSchedule _cron_schedule = new CronSchedule();
|
|
private readonly ThreadStart _thread_start;
|
|
private Thread _thread;
|
|
|
|
public CronJob(string schedule, ThreadStart thread_start)
|
|
{
|
|
_cron_schedule = new CronSchedule(schedule);
|
|
_thread_start = thread_start;
|
|
_thread = new Thread(thread_start);
|
|
}
|
|
|
|
private object _lock = new object();
|
|
public void execute(DateTime date_time)
|
|
{
|
|
lock (_lock)
|
|
{
|
|
if (!_cron_schedule.isTime(date_time))
|
|
return;
|
|
|
|
if (_thread.ThreadState == ThreadState.Running)
|
|
return;
|
|
|
|
_thread = new Thread(_thread_start);
|
|
_thread.Start();
|
|
}
|
|
}
|
|
|
|
public void abort()
|
|
{
|
|
_thread.Abort();
|
|
}
|
|
|
|
}
|
|
}
|