2010-03-31 96 views
0

我在我的.NET應用程序中使用Quartz。起初,我在Windows服務中使用它,但它不起作用,所以我將它移動到一個正常的項目中進行測試。這是主要的代碼:Quartz .NET不會觸發觸發器,爲什麼?

ISchedulerFactory schedFact = new StdSchedulerFactory(); 
IScheduler sched = schedFact.GetScheduler(); 
JobDetail jobDetail = new JobDetail("JobPrueba", null, typeof(JobPrueba)); 
Trigger trigger = TriggerUtils.MakeMinutelyTrigger(1, 3); 
trigger.StartTimeUtc = DateTime.Now; 
trigger.Name = "TriggerPrueba"; 
sched.Start(); 
sched.ScheduleJob(jobDetail, trigger); 

,這是JobPrueba:

class JobPrueba : IStatefulJob 
{ 
    public JobPrueba() { } 

    public void Execute(JobExecutionContext context) 
    { 
     const string fic = @"C:\prueba.txt"; 
     string texto = DateTime.Now.ToString(); 
     System.IO.StreamWriter sw = new System.IO.StreamWriter(fic, true); 
     sw.WriteLine(texto); 
     sw.Close(); 
     System.Console.WriteLine("Hello world"); 
    } 
} 

它沒有做任何事情,當執行主最後一行,程序永遠不會結束,但它不」寫入文件或在控制檯中打印Hello World。

有誰知道我在做什麼錯?

回答

3

我發現這是一個時代問題。我住的時間是UTC + 2的國家,所以當我將觸發器的StartTimeUtc設置爲DateTime.Now,而不是UtcNow時,觸發器在兩個小時後纔開始觸發,並且我認爲它必須觸發在代碼執行的那一刻。

此外,我設置了一個計時器以記錄發生了什麼,並在該日誌中打印了當前時間(使用DateTime.Now)和觸發器的StartTimeUtc,並且顯然它們是相同的,後來的DateTime.Now大於StartTimeUtc。如果我打印了DateTime.UtcNow,我已經看到了這個問題。

相關問題