运行 计算机关闭时的程序

Running a program while the computer is turned off

我做了一个程序。它每小时自动发布一次推文。我该怎么做才能让它在计算机关闭时工作。比如有个账号叫@everycolorbot,这个账号的工作逻辑是什么?

您可以使用 Azure Functions 执行此操作,您只需为计算时间付费:

  1. 创建一个新的 Functions project。在 Visual Studio 或在线。我的首选是 VS,因为我可以将代码保留在源代码管理中。您可能需要打开 Visual Studio 安装程序并安装 Azure 工具 - 无论哪种方式,请查看我刚刚在此处发布的 Azure Functions link 中的文档,以确保您使用的是最新信息。

  2. 创建 Functions 项目时,选择 Timer trigger

  3. 采用默认值,它将为您创建一个新的功能项目:

using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;

namespace TweetMyStuff
{
    public static class MyTweetBot
    {
        [FunctionName("MyTweetBot")]
        public static void Run([TimerTrigger("0 * * * *")]TimerInfo myTimer, ILogger log)
        {
            // your tweet logic here
        }
    }
}

请注意,我将 TimerTrigger 参数属性上的 chron 设置设置为 "0 * * * *" 这将在每小时整点启动该功能。 Azure 有 chron expression syntax documentation,它匹配 Linux 语法(因此您可以通过网络搜索找到更多信息)。

  1. 最后,部署和监控 - 您可以访问 QuickStart for Visual Studio 以帮助您朝着正确的方向开始。