gpt4 book ai didi

c# - Windows 任务计划程序或 WebApi 中的 TaskService 函数

转载 作者:可可西里 更新时间:2023-11-01 07:58:10 28 4
gpt4 key购买 nike

我想在 ASP.NET Web API 中创建一些函数,这些函数应该每天在特定时间执行并执行特定任务,如更新状态/记录/生成电子邮件、SMS。

我应该在代码中创建一个 TaskService

using System;
using Microsoft.Win32.TaskScheduler;

class Program
{
static void Main(string[] args)
{
// Get the service on the local machine
using (TaskService ts = new TaskService())
{
// Create a new task definition and assign properties
TaskDefinition td = ts.NewTask();
td.RegistrationInfo.Description = "Does something";

// Create a trigger that will fire the task at this time every other day
td.Triggers.Add(new DailyTrigger { DaysInterval = 2 });

// Create an action that will launch Notepad whenever the trigger fires
td.Actions.Add(new ExecAction("notepad.exe", "c:\\test.log", null));

// Register the task in the root folder
ts.RootFolder.RegisterTaskDefinition(@"Test", td);

// Remove the task we just created
ts.RootFolder.DeleteTask("Test");
}
}
}

或者我应该创建一个 .bat 文件并在任务计划程序中创建一个新任务。

最佳答案

正如您在问题中提到的,您需要执行特定任务,例如更新状态/记录/生成电子邮件、短信等。

因此数据库访问进入了场景,另一方面,您将不得不发送电子邮件和 SMS,这可能需要第三方库或其他配置设置访问权限。

因此,要做到这一切,最好进行代码实现,通过它您可以很好地维护您的更改和需求。

关于“.bat 文件和 windows 调度程序”,您需要熟练使用可用的有限批处理命令来满足您的要求

所以,我的建议是代码、.exe 和 windows 调度程序任务。

另外,这应该是一个单独的应用程序,不要将它与 Web API 代码混在一起。您始终可以使用 Web API 项目在 Web API 解决方案中创建一个新项目,并重用任何可能的代码。

关于c# - Windows 任务计划程序或 WebApi 中的 TaskService 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42139855/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com