简体   繁体   English

从 Web 应用程序运行计划任务

[英]Run a scheduled task from web application

I am using the following code to execute a Windows Scheduled Task from my web application.我正在使用以下代码从我的 Web 应用程序执行 Windows 计划任务。 Both the web application and the scheduled application runs on the same server. Web 应用程序和计划应用程序都运行在同一台服务器上。

var proc = new Process
{
    StartInfo =
    {
        UseShellExecute = false,
        FileName = @"C:\Windows\System32\schtasks.exe",
        Arguments = "/run /tn StartDataSync",
        UserName = "admin123",
        Password = passwd
    }
};
proc.Start();
proc.WaitForExit();

Now I would like to run the same scheduled application from a web application running on another machine in the same network domain.现在我想从在同一网络域中的另一台机器上运行的 Web 应用程序运行相同的计划应用程序。 When I researched I found like I can specify the name or IP address of the remote computer I want to connect to in the /S system argument .当我研究时,我发现我可以在 /S 系统参数中指定我想连接的远程计算机的名称或 IP 地址 So I tried the following code, but its not working.所以我尝试了以下代码,但它不起作用。

var proc = new Process
{
    StartInfo =
    {
        UseShellExecute = false,
        FileName = @"C:\Windows\System32\schtasks.exe",
        Arguments = "/run /S 192.168.5.202 /tn StartDataSync",
        UserName = "admin123",
        Password = passwd
    }
};
proc.Start();
proc.WaitForExit();

Try this answer ..according to this post试试这个答案..根据这篇文章

using TaskScheduler;

using (TaskService tasksrvc = new TaskService(server.Name, login, domain, password))
{
    Task task = tasksrvc.FindTask(taskName);
    task .Run();       
}

dll for task scheduler can be found here https://github.com/dahall/taskscheduler任务调度程序的 dll 可以在这里找到https://github.com/dahall/taskscheduler

Have considered a different alternative to scheduling the execution of an EXE directly from your web application?是否考虑过直接从 Web 应用程序调度 EXE 执行的不同替代方案?

For example, scheduling tasks from within an ASP.NET project is possible using the Revalee open source project.例如,可以使用Revalee开源项目从 ASP.NET 项目中调度任务。

Revalee is a service that allows you to schedule web callbacks to your web application. Revalee 是一项服务,允许您安排 Web 回调到您的 Web 应用程序。 In your case, you would schedule a callback that would synchronize your data at a specific time.在您的情况下,您将安排一个回调,在特定时间同步您的数据。 Revalee works very well with tasks that are discrete transactional actions, like updating some database values or sending an automated email message (read: not long running). Revalee 非常适用于离散事务操作的任务,例如更新某些数据库值或发送自动电子邮件消息(阅读:不长时间运行)。 The code to perform your action would all reside within your app.执行您的操作的代码都位于您的应用程序中。

To use Revalee, you would:要使用 Revalee,您将:

  1. Install the Revalee Service, a Windows Service, on your server.在您的服务器上安装 Revalee 服务(一种 Windows 服务)。 The Windows Service is available in the source code (which you would compile yourself) or in a precompiled version available at the Revalee website . Windows 服务在源代码(您可以自己编译)或Revalee 网站上提供的预编译版本中可用。

  2. Use the Revalee client library in your Visual Studio project.在 Visual Studio 项目中使用 Revalee 客户端库。 (There is an MVC-specific version too.) The client library is available in the source code (which, again, you would compile yourself) or in a precompiled version available via NuGet . (也有一个特定于 MVC 的版本。)客户端库在源代码中可用(同样,您可以自己编译)或通过NuGet提供的预编译版本。

  3. You would register a future callback when your code calls the ScheduleDataSynchronization() method (this example is assuming that you need your action to run 12 hours from now).当您的代码调用ScheduleDataSynchronization()方法时,您将注册一个未来的回调(此示例假设您需要您的操作在 12 小时后运行)。

     private void ScheduleDataSynchronization() { DateTimeOffset callbackTime = DateTimeOffset.Now.AddHours(12.0); // The callback should be in 12 hours from now Uri callbackUrl = new Uri(string.Format("http://mywebapp.com/SyncData.aspx")); // Register the callback request with the Revalee service RevaleeRegistrar.ScheduleCallback(callbackTime, callbackUrl); }
  4. When Revalee calls your application back, your app would perform whatever action you have coded it to do.当 Revalee 回调您的应用程序时,您的应用程序将执行您对其进行编码的任何操作。 You do this by adding the following method call ( SynchronizeData() ) to your SyncData.aspx page handler, à la:您可以通过将以下方法调用( SynchronizeData() )添加到您的SyncData.aspx页面处理程序来实现这一点,à la:

     private void SynchronizeData() { // TODO Lookup the job information and execute the data synchronization // ... return; }

I hope this helps.我希望这有帮助。

Note: The code example above uses a synchronous version of ScheduleCallback() , the Revalee client library also supports asynchronous calls à la:注意:上面的代码示例使用ScheduleCallback()的同步版本,Revalee 客户端库也支持异步调用à la:

RevaleeRegistrar.ScheduleCallbackAsync(callbackTime, callbackUrl);

In case it was not clear above, the Revalee Service is not an external 3rd party online scheduler service, but instead a Windows Service that you install and fully control on your own network.如果上面不清楚,Revalee 服务不是外部的第 3 方在线调度程序服务,而是您在自己的网络上安装完全控制的 Windows 服务。 It resides and runs on a server of your own choosing, most likely your web server (but this is not a requirement), where it can receive callback registration requests from your ASP.NET application.它驻留在并运行在您自己选择的服务器上,很可能是您的 Web 服务器(但这不是必需的),它可以从您的 ASP.NET 应用程序接收回调注册请求。

Disclaimer: I was one of the developers involved with the Revalee project.免责声明:我是参与 Revalee 项目的开发人员之一。 To be clear, however, Revalee is free, open source software.然而,需要明确的是,Revalee 是免费的开源软件。 The source code is available on GitHub .源代码可在GitHub找到

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM