如何在指定时间每天执行一个方法 [英] How to execute a method daily at a specified time

查看:78
本文介绍了如何在指定时间每天执行一个方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在该时间存储在变量中的特定时间执行方法。变量从数据库中获取时间,以便用户可以在数据库中指定该时间。如果将它与windows时间进行比较,程序会自动执行所述方法。

即使程序没有运行,也可以执行此方法。我的意思是像进程外服务或Windows服务。



虚假粗体和代码块被删除 - OriginalGriff [/ edit]



大家好!

感谢您的所有答案。让我们保持简单:我有一个方法,我希望用户能够在用户指定的时间执行。此时间应存储在数据库中,程序会自动检索该时间,并与该时间每天自动运行该方法的窗口时间进行比较。我不知道在程序运行时如何执行此操作,我只是想知道在程序未运行时是否可以执行此操作,但是在我打算编写的程序运行时让这项工作更重要。



再次感谢!

How can one execute a method at a specific time where that time is stored in a variable. The variable gets the time from a database so the user can specify that time in the database. If it is compared to the windows time the program automatically should execute said method.
Is it possible to have this method execute even if the program is not running. I mean something like an out of process service or a windows service.

[edit]Spurious bold and code block removed - OriginalGriff[/edit]

Hello everyone!
Thanks for all your answers. Let's keep it simple: I have a method I want the user to be able to execute at a time this user specified. This time should be stored in a database and the program automatically retrieves that time and it compares to the windows time the method should be run automatically once every day at that time. I don't know how to do this while the program is running and I was just wondering if this operation is possible while the program is not running, but it's more important to me to make this work while the program I intent to write is running.

Thanks again!

推荐答案

创建Windows服务很困难。如果您只是需要按计划运行某个应用程序,则已经创建了这样的服务。



它被称为Windows任务计划程序,请参阅http://en.wikipedia.org/wiki/Task_Scheduler [ ^ ]。



我有你可以使用的API。您可以使用v.1.0或v.2.0。



您可以使用与Windows捆绑的两个应用程序:AT.EXE(http://en.wikipedia.org/wiki/At_(Windows) [ ^ ])或Schtasks.exe( http://en.wikipedia.org/wiki/Schtasks [ ^ ])。这种方法不需要编程(好吧,也许只是一个批处理文件,因为命令行很难记住)。



-SA
Creating Windows Service is difficult. If you just need to run some application on schedule, such service is already created.

It is called Windows Task Scheduler, see http://en.wikipedia.org/wiki/Task_Scheduler[^].

I has its API you can use. You can use either v.1.0 or v.2.0.

You can use two applications bundled with Windows: AT.EXE (http://en.wikipedia.org/wiki/At_(Windows)[^]) or Schtasks.exe (http://en.wikipedia.org/wiki/Schtasks[^]). This approach does not require programming (well, maybe just a batch file because the command line is hard to remember).

—SA


我的建议是创建一个在机器启动时启动的Windows服务。此服务将使用Timer定期检查数据库或其他形式的数据持久性(xml文件,服务配置文件等),以便执行方法。如果时间过去了,那么你执行方法。



这是一个很好的教程创建Windows服务 [ ^ ]



希望这有帮助
My advice would be to create a Windows service that starts on machine startup. This Service would use a Timer to periodically check a database, or other form of data persistence(xml file, service config file, etc), for the time to execute your method. If the time elapses, then you execute the method.

Here is a good tutorial on Creating a Windows Service[^]

Hope this helps


正如 SA 建议的那样,任务计划程序可用于将程序设置为在特定时间运行。因为您正在寻找一个代码,但使用AT命令调度某些东西的优点是您的程序不需要一直在系统上运行。

任务调度程序将在正确的时间启动程序,它可以执行然后退出。您可能需要稍微调整一下程序以使其自动备份然后退出,但这应该是可能的。

实际上如何使用AT命令安排任务 [ ^ ]。完成此任务时,AT命令要好得多。
As SA suggested you, the task scheduler can be used to set programs to run at a specific time. as you are looking for a code for this but the advantage of scheduling something with the AT command is that your program does not need to remain running on the system all of the time.
The task scheduler will start your program at the right time, it can execute and then it exits. You might need to tweak your program a little to have it backup automatically and then exit but that should be possible.
Actually How To Use the AT Command to Schedule Tasks[^]. AT command is much better for accomplished this task.


这篇关于如何在指定时间每天执行一个方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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