2008-12-23 53 views
0

在我的應用程序中,數據存儲在.config文件中(以XML格式)。用戶可以設置他想要郵件的日期(如通過郵件提醒)。所以應該有一個日程安排程序,每天執行一次,以便將目標日期的郵件發送給用戶。由於沒有數據庫交互,所以如何運行調度程序?如何對存儲在.config文件中的數據執行schedular?

我對這個任務完全空白。誰能幫我?

在此先感謝。

回答

3

如果是時候執行任務,您可以定期在後臺運行應用程序。 Windows服務是我經常用於這類任務的東西。或者,您可以編程方式創建一個Windows計劃任務來運行您的應用程序。

+0

打我一拳! – cgreeno 2008-12-23 10:46:54

2

只需使用Windows調度程序服務即可運行任何exe,或者如果過程非常複雜,您可以創建自己的服務。

1

您可以使用最適合您的窗口調度程序或服務,但除此之外,您還可以通過Web應用程序在其中運行計劃任務。爲此,您必須使用goabal.asax文件。每60分鐘後將運行一次。 global.asax代碼如下::

<code> 
<%@ Application Language="C#" %> 

<script runat="server"> 

    private const string DeliveryPageUrl = "http://put any test url of your application"; 

    private const string DummyCacheItemKey = "Any hard coded value"; // you can put any name here DummyCacheItemKey = "gigigagagugu"; 

    void Application_Start(object sender, EventArgs e) 
    { 
     // Code that runs on application startup 
     RegisterCacheEntry(); 
    } 

    protected void Application_BeginRequest(Object sender, EventArgs e) 
    { 
     // If the dummy page is hit, then it means we want to add another item 
     // in cache 
     if (HttpContext.Current.Request.Url.ToString() == DeliveryPageUrl) 
     { 
      // Add the item in cache and when succesful, do the work. 
      RegisterCacheEntry(); 
     } 
    } 
    /// <summary> 
    /// Register a cache entry which expires in 60 minute and gives us a callback. 
    /// </summary> 
    /// <returns></returns> 
    private void RegisterCacheEntry() 
    { 
     // Prevent duplicate key addition 
     if (null != HttpContext.Current.Cache[DummyCacheItemKey]) return; 

     HttpContext.Current.Cache.Add(DummyCacheItemKey, "Test", null, DateTime.MaxValue, 
             TimeSpan.FromMinutes(60), CacheItemPriority.NotRemovable, 
             new CacheItemRemovedCallback(CacheItemRemovedCallback)); 
    } 
    /// <summary> 
    /// Callback method which gets invoked whenever the cache entry expires. 
    /// We can do our "service" works here. 
    /// </summary> 
    /// <param name="key"></param> 
    /// <param name="value"></param> 
    /// <param name="reason"></param> 
    public void CacheItemRemovedCallback(string key, object value, CacheItemRemovedReason reason) 
    { 
     // We need to register another cache item which will expire again in one 
     // minute. However, as this callback occurs without any HttpContext, we do not 
     // have access to HttpContext and thus cannot access the Cache object. The 
     // only way we can access HttpContext is when a request is being processed which 
     // means a webpage is hit. So, we need to simulate a web page hit and then 
     // add the cache item. 
     HitPage(); 
    } 

    /// <summary> 
    /// Hits a local webpage in order to add another expiring item in cache 
    /// </summary> 
    private void HitPage() 
    { 
     System.Net.WebClient client = new System.Net.WebClient(); 
     client.DownloadData(DeliveryPageUrl); 
    } 
    void Application_End(object sender, EventArgs e) 
    { 
     // Code that runs on application shutdown 

    } 

    void Application_Error(object sender, EventArgs e) 
    { 
     // Code that runs when an unhandled error occurs 

    } 

    void Session_Start(object sender, EventArgs e) 
    { 
     // Code that runs when a new session is started 

    } 

    void Session_End(object sender, EventArgs e) 
    { 
     // Code that runs when a session ends. 
     // Note: The Session_End event is raised only when the sessionstate mode 
     // is set to InProc in the Web.config file. If session mode is set to StateServer 
     // or SQLServer, the event is not raised. 

    } 

</script> 

    </code> 
+0

請在代碼中加入一些格式,以便我們可以更好地閱讀它 – 2008-12-23 18:16:21

相關問題