如何对 .config 文件中存储的数据执行计划?

发布于 2024-07-10 13:55:05 字数 172 浏览 9 评论 0原文

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

我对这个任务完全空白。 谁能帮我?

提前致谢。

In my application data is stored in a .config file,(in XML format). User is able to set date on which he wants mail (like reminder through mail). So there should be a scheduler which will execute daily to send mails on target date to users. As there is no database interaction how is it possible to run scheduler?

I am totally blank about this task. Can anyone help me?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

硪扪都還晓 2024-07-17 13:55:05

您可以让应用程序在后台运行,定期检查是否需要执行任务。 我经常使用 Windows 服务来执行此类任务。 或者,您可以以编程方式创建 Windows 计划任务来运行您的应用程序。

You can have an application running in background checking periodically if it is time to do a task. A windows service is what I use frequently for this kind of tasks. Or you can programatically create a windows scheduled task to run your app.

比忠 2024-07-17 13:55:05

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

Just use the windows scheduler service that will run any exe, alternatively if the process is significantly complex you could create your own service.

吾性傲以野 2024-07-17 13:55:05

您可以使用最适合您的窗口调度程序或服务,但除此之外,您还可以在 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>

you can use the window scheduler or service which suits you best but apart from that you have the option in web application also through which you can run the scheduled task. For that you have to use the goabal.asax file .It will run after every 60 minutes .
global.asax code is as follows ::

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文