几天来,我一直在努力完成这项工作。我正在尝试使用石英调度程序在我的主窗体上触发一个事件,从中可以调用一个方法。我的主窗体上的代码如下:
void StartSchedule()
{
string AuctionTime = "0 0 0 ? * MON-FRI *";
JobKey jobkey = new JobKey("Auction", "G");
IJobDetail job = JobBuilder.Create<AuctionJob>()
.WithIdentity(jobkey)
.Build();
CronScheduleBuilder csb = CronScheduleBuilder.CronSchedule(new CronExpression(AuctionTime)).InTimeZone(TimeZoneInfo.Local);
ICronTrigger trigger = (ICronTrigger)TriggerBuilder.Create()
.WithIdentity("Auction-Trigger", "G")
.WithSchedule(csb)
.Build();
// Create listener
JobListener auctionListener = new JobListener();
auctionListener.JobExecutedHandler += new EventHandler(SendAuctionOrders);
IMatcher<JobKey> matcher = KeyMatcher<JobKey>.KeyEquals(jobkey);
_scheduler.ListenerManager.AddJobListener(auctionlistener, matcher);
// Add to scheduler
DateTimeOffset ft = _scheduler.ScheduleJob(job, trigger);
LogEvent(job.Key + " scheduled at: " + ft.ToLocalTime());
jobkey = new JobKey("Auction2", "G");
IJobDetail job = JobBuilder.Create<AuctionJob>()
.WithIdentity(jobkey)
.Build();
csb = CronScheduleBuilder.CronSchedule(new CronExpression(AuctionTime)).InTimeZone(TimeZoneInfo.Local);
ICronTrigger trigger = (ICronTrigger)TriggerBuilder.Create()
.WithIdentity("Auction-Trigger", "G")
.WithSchedule(csb)
.Build();
// Create listener
JobListener auction2Listener = new JobListener();
auction2Listener.JobExecutedHandler += new EventHandler(SendAuctionOrders);
IMatcher<JobKey> matcher = KeyMatcher<JobKey>.KeyEquals(jobkey);
_scheduler.ListenerManager.AddJobListener(auction2listener, matcher);
// Add to scheduler
ft = _scheduler.ScheduleJob(job, trigger);
LogEvent(job.Key + " scheduled at: " + ft.ToLocalTime());
_scheduler.Start();
}
void SendAuctionOrders()
{
// Do something
}
AuctionJob 类如下所示:
public class AuctionJob : IJob
{
public void Execute(IJobExecutionContext context)
{
// Empty
Console.WriteLine("auction exec");
}
}
虽然作业侦听器看起来像这样:
public class JobListener : IJobListener
{
private static readonly ILog logger = LogManager.GetLogger(typeof(JobListener));
public event EventHandler JobExecutedHandler;
public void JobExecutionVetoed(IJobExecutionContext context)
{
logger.Info("JobExecutionVetoed");
}
public void JobToBeExecuted(IJobExecutionContext context)
{
logger.Info("JobToBeExecuted");
}
public void JobWasExecuted(IJobExecutionContext context, JobExecutionException jobException)
{
// Raise the event on executed
OnJobExecuted(EventArgs.Empty);
logger.Info("JobWasExecuted");
}
public string Name
{
get
{
return "JobListener";
}
}
// Event raiser
protected virtual void OnJobExecuted(EventArgs args)
{
// This code will prevent IllegalThreadContext exceptions
EventHandler jobExecHandler = JobExecutedHandler;
if (jobExecHandler != null)
{
ISynchronizeInvoke target = jobExecHandler.Target as ISynchronizeInvoke;
if (target != null && target.InvokeRequired)
{
target.Invoke(jobExecHandler, new object[] { this, args });
}
else
{
jobExecHandler(this, args);
}
}
}
}
当作业被触发时,作业被执行(控制台打印“auction exec”)但作业侦听器的 JobWasExecuted 没有拾取它。谁能帮我解决这个问题?
附加问题:还有其他方法可以调用主窗体的方法吗?此外,作业侦听器类是否会在整个应用程序中持续存在(理论上不会关闭)。谢谢。