当一个批次通过鼠标关闭检测或拦截时刻(X控制台按钮) [英] Detect or intercept moment when a batch is closed via mouse (X console button)

查看:132
本文介绍了当一个批次通过鼠标关闭检测或拦截时刻(X控制台按钮)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要运行一个批处理脚本A,这是仅运行或者当批次通过你想退出[Y / N]问题批次内或当所有的脚本结束编程关闭。

I need to run a batch "Script A" and this is only run either when the batch is closed programatically via the "Do you want to exit [y / n] questions" or when all the scripts within the batch are concluded.

如果任何时候用户决定通过鼠标按钮关闭该批次如果批次崩溃,剧本A会,可惜的是,不被执行的(在控制台的上部右侧的X)。

If anytime the user decides to close the batch via the mouse button ("X" on the upper right-hand side of the console) of if the batch crashes, "Script A" will, unfortunatelly, not be executed.

我如何编程拦截的X任何时候,启动另一批敲定的事?

How can I programatically intercept any moment of "X" and launch another batch to finalize the deed?

亲切的问候,

Maleck

推荐答案

下面这个简单的方案似乎工作。清理脚本调用通过与/ WAIT选项START命令的主要脚本。这将确保控制将返回关闭窗口即使孩子的过程中死亡。

The following simple scheme seems to work. The cleanup script calls the main script via the START command with the /WAIT option. This ensures control will return even if the child process is killed by closing the window.

如果你想的父清理脚本通常会提示终止批处理作业(Y / N)?的如果主脚本关闭窗口丧生。我管N转换START命令来自动回答提示,它应该出现。标准错误的外块重定向NUL燮presses丑 ^ C (控制-C),显示了当被杀害主窗口。

The parent cleanup script will normally prompt if you want to "Terminate batch job (Y/N)?" if the main script is killed by closing the window. I pipe N into the START command to automatically answer that prompt, should it arise. The outer block redirection of stderr to nul suppresses the ugly ^C (Control-C) that shows up when the main window is killed.

主要的批处理文件不能直接启动,而是通过CMD / C命令启动。这样可以确保如果主脚本由pressing控制-C杀死控制甚至会回来。这也消除了需要放在主脚本EXIT指令(可能永远不会被执行的任何方式)。

The main batch file isn't STARTed directly, but instead is launched via the CMD /C command. This ensures control will return even if the main script is killed by pressing Control-C. This also eliminates the need to put an EXIT command in the main script (which might never be executed any way).

cleanup.bat

@echo off
:: Launch the main program and don't continue until it completes (or is killed)
2>nul (
  echo N|start /wait "" cmd /c main.bat
)

:: Cleanup up begins here
echo Time to wrap it up!

下面是一个简单的 main.bat 以测试返回功能

Here is a trivial main.bat to test the return functionality

@echo off
echo This is the main script.
echo(
echo    Press any key to close normally
echo or Close this window via the console window X button
echo or Press Ctrl-C to break out of this script abnormally
pause >nul


修改


EDIT

当然没有什么prevent从主脚本终止之前关闭cleanup.bat窗口用户,它可让您立即回到了起点: - (

Of course there is nothing to prevent a user from closing the "cleanup.bat" window before the main script terminates, which puts you right back to square one :-(

您可以在没有任何可见的窗口关闭运行通过WScript的一个VBScript或JScript。然后,(VB / J)脚本可以启动main.bat脚本,然后推出一个cleanup.bat脚本只有当主进程结束(或者被杀死)。这将使其更难以为最终用户与拉开清理脚本的过程干扰

You can run a VBScript or Jscript via wscript without any visible window to close. The (VB/J)script can then launch the main.bat script and then launch a cleanup.bat script only when the main process finishes (or is killed). That would make it more difficult for an end user to interfere with the process of kicking off the cleanup script.

master.vbs

Set WshShell = CreateObject("WScript.Shell")
WshShell.Run "D:\test\main.bat", 1, TRUE
WshShell.Run "D:\test\cleanup.bat", 1, TRUE
Set WshShell = Nothing

但是,一旦你越过那条threshould,为什么不这样做的(VB / J)脚本?

But once you cross that threshould, why not do the entire project in (VB/J)script?

这篇关于当一个批次通过鼠标关闭检测或拦截时刻(X控制台按钮)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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