如何对存储在.config文件中的数据执行调度程序?

时间:2008-12-23 10:37:01

标签: c# asp.net scheduler

在我的应用程序中,数据存储在.config文件中(XML格式)。用户可以设置他想要邮件的日期(如通过邮件提醒)。因此应该有一个调度程序,它将每天执行以在目标日期向用户发送邮件。由于没有数据库交互,如何运行调度程序?

我完全不了解这项任务。谁能帮我?

提前致谢。

3 个答案:

答案 0 :(得分:3)

如果是时候执行任务,您可以定期在后台运行应用程序。我经常使用Windows服务来完成这类任务。或者,您可以以编程方式创建Windows计划任务来运行您的应用程序。

答案 1 :(得分:2)

只需使用将运行任何exe的Windows调度程序服务,或者如果进程非常复杂,您可以创建自己的服务。

答案 2 :(得分: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>