Home > Backend Development > C++ > How to Create Scheduled Tasks in C# WPF using Task Scheduler Managed Wrapper?

How to Create Scheduled Tasks in C# WPF using Task Scheduler Managed Wrapper?

Barbara Streisand
Release: 2025-01-25 11:36:09
Original
662 people have browsed it

How to Create Scheduled Tasks in C# WPF using Task Scheduler Managed Wrapper?

Use the task plan program to host the packaging device to create a regular task in C# WPF

In your C# WPF project, you can use the task plan program to host the packaging to create and add timing tasks. The following is the implementation method:

instructions and references:

Code example:

<code class="language-csharp">using System;
using Microsoft.Win32.TaskScheduler;</code>
Copy after login

Other choices:

You can also use the native API to create timing tasks. In addition, Quartz.net is another population that schedules operations in the .NET application.
<code class="language-csharp">public static void CreateScheduledTask()
{
    // 获取本地计算机上的服务
    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));

        // 在根文件夹中注册任务
        ts.RootFolder.RegisterTaskDefinition(@"TestTask", td);

        // 删除刚刚创建的任务
        ts.RootFolder.DeleteTask("TestTask");
    }
}</code>
Copy after login

The above is the detailed content of How to Create Scheduled Tasks in C# WPF using Task Scheduler Managed Wrapper?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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