如何将我的 c# 程序作为计划任务运行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14562792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I run my c# program as a scheduled task
提问by Matthew P
I am still pretty much new to c# so you will have to bear with me.
我对 c# 还是很陌生,所以你必须忍受我。
I have developed a windows form program which updates some SQL records as an end of day process for one of our clients.
我开发了一个 windows 窗体程序,它更新一些 SQL 记录作为我们的一个客户的一天结束过程。
The next step is that I need to install the program on a server and simulate a button click in the program to become a scheduled task.
下一步是我需要将程序安装在服务器上并模拟程序中的按钮单击以成为计划任务。
I know how to setup the task on the server side where you start program and enter the arguments. But I am unsure as to what code I need to include in my program to achieve this.
我知道如何在启动程序并输入参数的服务器端设置任务。但我不确定我需要在我的程序中包含哪些代码来实现这一点。
采纳答案by Grant Thomas
My recommendation would be to get away from running a GUI-based/windowed application from a scheduled task - this is generally madness in practice. Ideally, deploy a console-based version of your application that requires execution (perhaps with parameter arguments) and doesn't require any user (or quasi-user-) interaction.
我的建议是避免从计划任务运行基于 GUI/窗口的应用程序 - 这在实践中通常是疯狂的。理想情况下,部署需要执行(可能带有参数参数)并且不需要任何用户(或准用户)交互的基于控制台的应用程序版本。
If you simply can'tcreate a 'system version' of your application, then I guess you have two choices, both immensely ugly: 1) create some kind of macro script which is executed instead of your program, this script could execute the program and issue 'the click', 2) perform 'the click' on startup of your application by invoking the button click handler (maybe based on a parameter to give it a duality in execution modes.)
如果您根本无法创建应用程序的“系统版本”,那么我想您有两种选择,两者都非常丑陋:1)创建某种宏脚本来代替您的程序执行,该脚本可以执行该程序并发出“点击”,2)通过调用按钮点击处理程序在应用程序启动时执行“点击”(可能基于一个参数,使其在执行模式中具有双重性。)
回答by Jakub Konecki
Consider using Windows Task Scheduler.
考虑使用Windows 任务计划程序。
You could extract your business logic to a separate DLL and write a simple Console app that will just run your task after accepting the parameters through command line.
您可以将您的业务逻辑提取到一个单独的 DLL 并编写一个简单的控制台应用程序,该应用程序将在通过命令行接受参数后运行您的任务。
回答by zabulus
回答by Sergey Gavruk
Very popular solution is Quartz.NET http://quartznet.sourceforge.net/
非常流行的解决方案是 Quartz.NET http://quartznet.sourceforge.net/
回答by Peter K.
I think you are also asking about command-line argument passing. See the answers to this question.
我认为您也在询问命令行参数传递。请参阅此问题的答案。
In particular, I highly recommend the accepted answer: NDesk.Options.
特别是,我强烈推荐接受的答案:NDesk.Options。
回答by Kiran1016
I have similar task to do making winforms as windows task. what i did is
我有类似的任务来制作 Winform 作为 Windows 任务。我所做的是
in windows task scheduler
in the task tab
,under Run
put your exe and then /Auto,it will run as schedule.
在windows task scheduler
中task tab
,下Run
把你的exe文件,然后/自动,它将运行的时间表。
Example:winform.exe /Auto
示例:winform.exe /Auto
回答by Wim Ombelets
If I'm understanding your question correctly, this is how you could possibly proceed:
如果我正确理解您的问题,这就是您可能继续的方式:
- Best way to parse command line arguments in C#?-> check the answers and choose a library to process the args or write your own code to do so.
- Create a scheduled task if those arguments are present by Creating Scheduled Tasks
- 在 C# 中解析命令行参数的最佳方法?-> 检查答案并选择一个库来处理参数或编写自己的代码来执行此操作。
- 如果通过创建计划任务存在这些参数,则创建计划任务
回答by Varinder Singh
Why not extract your database update logic as a windows service
为什么不将您的数据库更新逻辑提取为Windows 服务
you can segregate the sql handling part in a separate DLL
and use the common DLL
for both your form application and the windows service.
您可以将 sql 处理部分单独分开,DLL
并DLL
为您的表单应用程序和 Windows 服务使用公共部分。
A window service run in background and can be automatically started when the computer boots, can be paused and restarted, and do not show any user interface.
后台运行的窗口服务,可以在计算机启动时自动启动,可以暂停和重新启动,并且不显示任何用户界面。
Moreover you need not to install any third party software for same and window service code base can be ported to any windows machine with required version of .Net Framework
installed.
此外,您无需为此安装任何第三方软件,并且可以将窗口服务代码库移植到任何.Net Framework
安装了所需版本的 Windows 机器上。
回答by George Eye
If it is a windows application, just go to the bin folder, get the executable file, and finally schedule a task for it by using windows schedule task and choose the exe file as you targeted application.
如果是windows应用程序,只需进入bin文件夹,获取可执行文件,最后使用windows schedule task为其安排任务,选择exe文件作为目标应用程序。
if it is web application, you may want to include your code in a quartz.net scheduled job, details are on quartz.net website.
如果它是 Web 应用程序,您可能希望将您的代码包含在 Quartz.net 计划作业中,详细信息在quartz.net 网站上。
回答by mohammadrg
Add reference: Microsoft.Win32.TaskScheduler
then write this code:
添加引用: Microsoft.Win32.TaskScheduler
然后编写此代码:
using (TaskService ts = new TaskService())
Microsoft.Win32.TaskScheduler.Task task = ts.GetTask(TaskName);
task.Run(); //start
task.Stop(); //End