你如何等待一个任务计划任务批处理文件或C#来完成? [英] How do you wait on a Task Scheduler task to finish in a batch file or C#?

查看:167
本文介绍了你如何等待一个任务计划任务批处理文件或C#来完成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个批处理文件做两件事情:

I am trying to write a batch file that does two things:


  1. 首先,它启动其安装程序(的Program.exe)。安装程序(INSTALL.EXE)

  2. 二是启动安装程序(的Program.exe)的一个实例。安装完成后,此必须执行。

这是除了安装程序需要管理员权限相对简单,而且必须在用户上下文中运行。即使有这些限制,这仍然是除了相对简单,我上的Azure工作者角色,这意味着两件事运行以下命令:

This would be relatively straightforward except that the installer needs administrator privileges and must run in a user context. Even with these restrictions this would still be relatively straightforward except that I am running this on an Azure Worker Role, which means two things:


  1. 高架批处理文件必须从启动任务运行。

  2. 没有为启动任务没有用户上下文在Azure的辅助角色。

  1. Elevated batch files must be run from a startup task.
  2. There is no user context for startup tasks in Azure worker roles.

因此​​,似乎该解决方案是运行安装程序为一个真正的用户上下文一个任务计划任务。然而,这种带有告诫我会再需要等待这个任务完成之前,我可以发动的Program.exe。

It therefore appears that the solution is to run the installer as a Task Scheduler task in a real user context. However, this comes with the caveat that I would then need to wait for this task to finish before I could launch program.exe.

因此​​,我的问题是:我如何等待一个任务计划任务批处理文件来完成

Thus, my question is: How do I wait on a Task Scheduler task to finish in a batch file?

另外,我可以菊花链在一个单一的任务计划任务中我的批处理文件,这两个电话(任务计划最多在一个任务16个连续的事件支持[来源请求])。

Alternatively, I could daisy chain the two calls in my batch file within a single Task Scheduler task (Task Scheduler supports up to 16 sequential events in a single task [Citation Needed]).

不过,如果我采取这种方法我的批处理文件将尽快任务是终止的计划的,不是只要它的完成的。不幸的是,我必须等待它完成,然后我可以在我的工作者角色做任何的逻辑。不过,如果我可以检查一个任务计划程序任务已经从C#(没有管理员权限)完成了,那么我就可以等待它。

However, if I take this approach my batch file will terminate as soon as the task is scheduled, not as soon as it finishes. Unfortunately, I must wait on it to finish before I can do any of the logic in my Worker Role. However, if I can check if a Task Scheduler task has finished from C# (WITHOUT admin privileges), then I can just wait on it there.

因此​​,一个可行的第二个答案将是一个问题:我如何检查,如果一个任务计划程序任务已经从C#完成

Thus a second viable answer would be to the question: How do I check if a Task Scheduler task has completed from C#?

编辑:我想我会通过MC ND澄清如下回答了一下:

从一个较高的水平,这个命令检查是否正在运行的任务,然后睡觉,循环,如果它是。

From a high level, this command checks to see if the task is running, and then it sleeps and loops if it is.

:loop
for /f "tokens=2 delims=: " %%f in ('schtasks /query /tn yourTaskName /fo list ^| find "Status:"' ) do (
    if "%%f"=="Running" (
        ping -n 6 localhost >nul 2>nul
        goto loop
    )
)

呼叫的schtasks /查询/ TN yourTaskName / FO列表输出类似以下内容:

Folder: \  
HostName:      1234567890  
TaskName:      \yourTaskName  
Next Run Time: 11/27/2030 11:40:00 PM  
Status:        Ready  
Logon Mode:    Interactive/Background 

在超过输出的每一行命令循环。每一行被分成使用定界符两个记号:。如果发现启动了状态的线,则在该行的分隔符之后的令牌存储在变量f(这在例如输出上述将是准备就绪)。

The for command loops over each line in the output. Each line is split into two tokens using the delimiter ":". If a line starting with "Status" is found, then the token after the delimiter in that line is stored in the variable f (which in the example output above would be "Ready").

变量f,然后检查,看它是否状态为正在运行相匹配。如果状态未运行,假设在任务完成或失败,并退出循环。如果状态为运行,然后在任务仍在运行和批处理脚本再次检查之前暂停的时间很短。这是通过使一个平呼叫完成的,因为批量不具有睡眠命令。一旦脚本完成睡它重新启动该序列。

The variable f is then checked to see if it matches the status "Running". If the status is not "Running" it is assumed that the task has completed or failed, and the loop exits. If the status is "Running", then the task is still running and the batch script pauses for a short time before checking again. This is accomplished by making a ping call, since batch does not have a sleep command. Once the script is done sleeping it restarts this sequence.

推荐答案

从批处理文件,查询任务状态,如果它正在运行,继续查询

From batch file, query the task status, and if it is running, keep querying

:loop
for /f "tokens=2 delims=: " %%f in ('schtasks /query /tn yourTaskName /fo list ^| find "Status:"' ) do (
    if "%%f"=="Running" (
        ping -n 6 localhost >nul 2>nul
        goto loop
    )
)

这篇关于你如何等待一个任务计划任务批处理文件或C#来完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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