c#中特定代码的周期性执行 [英] Periodic execution of particular code in c#

查看:69
本文介绍了c#中特定代码的周期性执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组我只想每天执行一次的查询,我知道在 C# 中使用 TaskScheduler 可以做到这一点.但我没有得到任何适合我要求的例子.有人可以为此提供示例代码吗?

I've a set of queries which I want to execute only once in day, I know this is possible using TaskScheduler in C#. But I am not getting any example suitable for my requirements. Can anybody give a example code for this?

推荐答案

你可以试试FluentScheduler.该文档包含您需要的所有示例代码.首先,我认为它仅适用于 Web,但现在我认为您也可以将其用于桌面应用程序.但不确定且未测试.

You can try FluentScheduler. The documentation has the sample codes all you need. Firstly I thought it is for web only, but now I think you can use it for using with Desktop Application too. But not sure and not tested.

https://fluentscheduler.codeplex.com/documentation

编辑您也可以使用任务计划程序 -

EDIT You can also use Task Scheduler -

  1. 首先创建一个可以运行和执行所有任务的控制台应用程序.您甚至可以使用它调用其他进程.然后构建可执行文件并将其保存在安全位置.

  1. First create a console application that can run and do all your tasks. You can even invoke other processes with it. Then build the executable and save it in a safe location.

然后转到管理工具 > 任务调度器,然后单击操作 > 新建任务创建一个新任务.您将看到这样的屏幕 -

Then go to Administrative Tools > Task Sheduler And create a new task by clicking Action > New Task. You will see a screen like this -

在那里选择您的可执行文件和其他权限.

Select your executable and other permissions there.

  1. 现在要按计划运行它,请移至下一个标签触发器",然后单击底部的添加.你会看到这样的画面 -
  1. Now to run it in schedule move to next tab 'Triggers' and click add at the bottom. You will see a screen like this -

现在添加您想要的时间表.确保使用日志,因为您将无法直接看到输出.为方便起见,您可以使用 Windows 事件查看器或写入自定义文本文件.

Now add your desired schedules. Make sure you use logs, because you will not be able to see the outputs directly. Either you can use windows event viewer or write to custom text file for your convenience.

任务计划程序是 Windows 本身的一部分.它没有依赖于 C# 或 C++ 任何东西.基本上你告诉windows它将定期运行特定程序.它的工作是执行程序以初始化所有环境并执行适当的代码.因此,即使您使用任务调度程序,您也必须确保您用来运行的程序,具有所有其他选项和代码没错.

Task Scheduler is a part of windows itself. It does not have a dependency on C# or C++ anything. Basically you tell windows that it will run the specific program at a regular schedule. It is the job of the executed program to initialize all environment and execute appropriate code. So even if you use task scheduler you have to make sure that the program you are using to run with it, has all other options and codes right.

这篇关于c#中特定代码的周期性执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆