C# - 替代System.Timers.Timer,调用一个函数在某一特定时间 [英] C# - Alternative to System.Timers.Timer, to call a function at a specific time

查看:1648
本文介绍了C# - 替代System.Timers.Timer,调用一个函数在某一特定时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要呼吁在特定的时间我的C#应用​​程序特定的功能。起初我想过使用一个定时 (System.Time.Timer),但很快就无法使用。为什么呢?

I want to call a specific function on my C# application at a specific time. At first I thought about using a Timer (System.Time.Timer), but that soon became impossible to use. Why?

简单。定时器类需要一个间隔以毫秒为单位,但考虑到我可能要执行的功能,让我们说,在一个星期内,这将意味着:

Simple. The Timer class requires a Interval in milliseconds, but considering that I might want the function to be executed, let's says in a week that would mean:


  • 7天= 168小时;

  • 168小时= 10,080分钟;

  • 10,080分=604800秒;

  • 604800秒= 604800000毫秒;

  • 所以区间为604800000;

  • 7 days = 168 hours;
  • 168 hours = 10,080 minutes;
  • 10,080 minutes = 604,800 seconds;
  • 604,800 seconds = 604,800,000 milliseconds;
  • So the interval would be 604,800,000;

现在,让我们记住,间隔接受的数据类型为 INT 和我们所知道 INT 范围去从-2,147,483,648到2,147,483,647。

Now let's remember that the Interval accepted data type is int, and as we know int range goes from -2,147,483,648 to 2,147,483,647.

<强>这使得定时器无用,而不是在此情况下,但在超过约25的情况下天,一旦我们不能设置间隔更大的2,147,483,647毫秒。

That makes Timer useless, not in this case, but in the case of more than about 25 days, once we cannot set a Interval bigger that 2,147,483,647 milliseconds.

所以我需要一个解决方案,我可以指定当函数被调用。的事情是这样的:

So I need a solution where I could specify when the function should be called. Something like this:

solution.ExecuteAt = "30-04-2010 15:10:00";
solution.Function = "functionName";
solution.Start();



所以,当系统时间将达到30-04-2010 15时10分00秒功能将在应用程序中执行。

So when the System Time would reach "30-04-2010 15:10:00" the function would be executed in the application.

这怎么解决?

其他信息:什么将这些功能做


  • 获取气候信息,并根据这些信息:

  • 启动/关闭其他应用程序(其中大多数是基于控制台);

  • 发送自定义命令那些控制台应用程序;

  • 掉电,重启,睡眠,休眠计算机;

  • 如果可能的时间表BIOS到计算机电;

  • Getting climate information and based on that information:
  • Starting / Shutting down other applications (most of them console based);
  • Sending custom commands to those console applications;
  • Power down, rebooting, sleep, hibernate the computer;
  • And if possible schedule the BIOS to power up the computer;

编辑:

这似乎是在间隔接受的数据类型为双击,但是如果你设置一个值越大,一个 INT 间隔和呼叫开始()它抛出一个例外 [0,Int32.MaxValue]

It would seem that the Interval accepted data type is double, however if you set a value bigger that an int to the Interval, and call Start() it throws a exception [0, Int32.MaxValue].

编辑2:

约恩休乌 - 罗德使用建议 Ncron 处理计划任务,并在第一次看这似乎是一个很好的解决方案,但我想听到一些谁一直与它的工作。

Jørn Schou-Rode suggested using Ncron to handle the scheduling tasks, and at first look this seems a good solution, but I would like to hear about some who has worked with it.

推荐答案

一种方法任务调度,这类似于通过 klausbyskov 提出的,是内置的调度服务上现有的.NET调度框架/库顶。相较于使用Windows任务调度,这具有的优势(一)允许在同一个项目和(b),保持工作和调度逻辑合被定义几份工作 - 即不依赖于服务器的设置容易迷失在。系统升级/替换

One approach to task scheduling, simliar to that proposed by klausbyskov, is to built your scheduling service on top of an existing .NET scheduling framework/library. Compared to using the Windows Task Scheduler, this has the advantages of (a) allowing several jobs to be defined in the same project and (b) keeping jobs and scheduling logic "together" - i.e. not relying on server settings prone to get lost in system upgrades/replacements.

我知道提供这种功能的两个开源项目:

I know of two open-source projects that offer this kind of functionality:


  • Quartz.NET 是可以使用一个全功能的,开源的作业调度系统从最小的应用程序来大型企业系统。我从来没有实际使用这个框架我自己,但是从研究的网站,我有一个非常坚实的工具的印象,提供了许多很酷的功能。事实上,有 [石英网] 标签上计算器可能也表明它在野外实际使用。

  • "Quartz.NET is a full-featured, open source job scheduling system that can be used from smallest apps to large scale enterprise systems." I have never actually used this framework myself, but from studying the website, I have the impression of a very solid tool, providing many cool features. The fact that there [quartz-net] tag on Stackoverflow might also indicate that it is actually used in the wild.

NCron 是建立和.NET服务器平台上部署计划的后台作业一个轻量级的图书馆。它没有一半多的特点Quartz.NET,它不会对任何#1标签,但笔者(你真心地)认为,其低摩擦的API使得它比较容易上手。

"NCron is a light-weight library for building and deploying scheduled background jobs on the .NET server platform." It does not have half as many features as Quartz.NET, and it does not have any tag on Stackoverflow, but the author (yours truly) believes that its low-friction API makes it somewhat easier to get started with.

在NCron之上建立的调度服务,可以安排一个 CleanupJob 使用一个单一的代码行每周执行:

Building your scheduling service on top of NCron, you can schedule a CleanupJob for weekly execution using a single line of code:

service.Weekly().Run<CleanupJob>();



<子>好吧,你将需要大约三行锅炉板代码,最重要的是实际把你的项目到一个Windows服务,但是当我声称,它可以用一行代码来完成它听起来更令人印象深刻;)

这篇关于C# - 替代System.Timers.Timer,调用一个函数在某一特定时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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