2010-06-07 136 views
9

我在應用程序中使用Quartz.NET。什麼是處理Quartz.NET的正確方法。正確的方式來處理Quartz.NET?

現在我只是做

if (_quartzScheduler != null) 
    { 
     _quartzScheduler = null; 
    } 

這就夠了,或者我應該落實在jobType類處置或東西嗎?

賽斯

+4

設置爲NULL沒有任何用處的.NET。 – 2010-06-07 19:20:25

+0

與處理對象無關 – 2010-06-07 19:23:57

+0

btw ...不需要檢查_quartzScheduler爲null,只需設置它...最終結果相同並且代碼更清晰 – MaLio 2010-06-10 20:09:23

回答

11
scheduler.Shutdown(waitForJobsToComplete: true); 

當然,如果你不是在C#4.0然而,命名參數不起作用:

scheduler.Shutdown(true); 
0

的文檔不說的IScheduler實現IDisposable的事情。如果您有自定義作業類型來抓取和保存資源(文件鎖定,數據庫連接),則可以在對象上實現IDispoable並覆蓋Dispose()以釋放資源。

1

這不是一個完整的例子,但可能會讓你走上正確的道路。我想實現這樣的:通過實現你的代碼,而繼承的IDisposable的功能,你可以再我們

public static void Main() 
{ 
    using (customSchedulerClass myScheduler = new customSchedulerClass()) 
    { 
     c.scheduleSomeStuff(); 
    } 
    console.WriteLine("Now that you're out of the using statement the resources have been disposed"); 
} 

所以基本上:

class customSchedulerClass : IDisposable 
{ 

    private Component component = new Component(); 
    private bool disposed = false; 

    public void scheduleSomeStuff() 
    { 
     //This is where you would implement the Quartz.net stuff 
    } 

    public void Dispose() 
    { 
     Dispose(true); 
     GC.SupressFinalize(this); 
    } 

    private void Dispose(bool disposing) 
    { 
     if(!this=disposed) 
     { 
      if(disposing) 
      { 
       component.dispose; 
      } 
     } 
     disposed = true; 
    }  
} 
這個

然後你就可以做使用之類的語句很酷的東西using聲明,當你完成它將乾淨地處理你的資源,並保持乾淨和漂亮的事情。 (免責聲明,這不是一個完整的例子,只是爲了讓你在正確的方向)。

0

通常我們不需要將對象設置爲null以便將其丟棄。 如果一個對象包含非託管資源,那麼它應該實現IDisposable(並由其所有客戶端調用)。

您可以參考this類似的帖子。

相關問題