Home > Backend Development > C++ > How Can I Create and Schedule Tasks in C# WPF Using the Task Scheduler?

How Can I Create and Schedule Tasks in C# WPF Using the Task Scheduler?

Linda Hamilton
Release: 2025-01-25 11:21:11
Original
509 people have browsed it

How Can I Create and Schedule Tasks in C# WPF Using the Task Scheduler?

Create and dispatch the task in the Windows task scheduling program in the C# WPF project

This article introduces how to create and schedule the task in the Windows task scheduling program in the C# WPF project.

Use the task scheduling program to host the packaging device

Use the task scheduling program to host the packaging and interact with the Windows task scheduling program:

using System;
using Microsoft.Win32.TaskScheduler;

class Program
{
    static void Main(string[] args)
    {
        using (TaskService ts = new TaskService())
        {
            TaskDefinition td = ts.NewTask();
            td.RegistrationInfo.Description = "执行特定操作";

            td.Triggers.Add(new DailyTrigger { DaysInterval = 2 }); // 每两天执行一次
            td.Actions.Add(new ExecAction("notepad.exe", "c:\test.log", null)); // 打开记事本并显示c:\test.log文件

            ts.RootFolder.RegisterTaskDefinition(@"TestTask", td); // 注册任务,名称为"TestTask"
            ts.RootFolder.DeleteTask("TestTask"); // 删除任务
        }
    }
}
Copy after login
Other choices

In order to control more fine control, you can consider using the native API; or use Quartz.net, which is a lightweight scheduling framework.

The above is the detailed content of How Can I Create and Schedule Tasks in C# WPF Using the Task Scheduler?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template