【问题标题】:How do I recall method daily even after reboot?即使重新启动后,我如何每天回忆方法?
【发布时间】:2021-09-16 16:42:53
【问题描述】:

我知道我用Task.Delay cam schedule方法,如果当时PC /应用程序关闭,我如何调用方法?

例如,如果我将方法安排在 18.00 并且我的 PC 处于打开状态,则调用该方法,如果当时 PC / 应用程序关闭,则当我启动我的 PC / 启动程序时将调用该方法.

【问题讨论】:

    标签: c# winforms methods scheduled-tasks


    【解决方案1】:

    你需要两件事:

    1. 您需要将事件信息保存在某处,例如文件系统中的数据库或平面文件。此信息必须包括您希望事件触发的日期和时间以及它需要的任何参数。

    2. 您需要某种方式在重新启动后自动启动程序。例如,您可以使用service 运行引导程序,或使用Task Scheduler 启动应用程序。

    【讨论】:

      【解决方案2】:

      您只需要最后一次保存(在文件中甚至app.config)并在您的应用程序启动时读取它。

      app.config文件中,例如,你应该添加新的键值设置:

          <add key="lastRecallTime" value="2021.07.06 01:02:03" />
      

      要保存最后一个DateTime 值,请使用此方法(其中键为“lastRecallTime”):

          private void SetAppConfig(string key, string value)
          {
              Configuration configuration = 
              ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
              configuration.AppSettings.Settings[key].Value = value;
              configuration.Save(ConfigurationSaveMode.Full, true);
              ConfigurationManager.RefreshSection("appSettings");
          }   
      

      app.config读取值:

          DateTime lastRecallTime = DateTime.Parse(ConfigurationManager.AppSettings["lastRecallTime"]);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-12-21
        • 2020-08-19
        • 1970-01-01
        • 1970-01-01
        • 2012-09-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多