简体   繁体   English

如何制作一个批处理文件来检测程序何时关闭然后重新启动应用程序?

[英]How to make a batch file that detects when a program closes and then restarts the application?

Is there a way that I can make a program re-open itself when it closes?有没有办法让程序在关闭时重新打开?

I have this program that runs;我有这个运行的程序; and it gets a pop up;它会弹出; when you press okay it closes the program.当您按确定时,它会关闭程序。 I know I can start any executable with a batch, but how would I make it run all on it's own when it detects the program has closed?我知道我可以用批处理启动任何可执行文件,但是当它检测到程序已关闭时,我如何让它自行运行?

I am able to start the program with this code:我可以使用以下代码启动程序:

@echo off
start "" "C:\Program Files (x86)\Winstep\WinstepXtreme.exe"

But I want this batch file to launch when WinstepXtreme.exe terminates.但我希望这个批处理文件在WinstepXtreme.exe终止时WinstepXtreme.exe

UPDATE: okay so the suggested code works but its activating non stop making the PC unable to do certain thing's, also the cmd window stay opne but i fixed that.更新:好的,所以建议的代码有效,但它的激活不停使 PC 无法做某些事情,cmd 窗口也保持打开状态,但我修复了这个问题。 in my process list there are two files: Nextstart.exe and Workshelf.exe, not sure if there is a code command where you can type, if "workshelf.exe" doe sent exist then start workshelf.exe?在我的进程列表中有两个文件:Nextstart.exe 和 Workshelf.exe,不确定是否有可以键入的代码命令,如果发送的“workshelf.exe”确实存在,则启动 workshelf.exe? If Not Exist "%ProgramFiles(x86)%" Goto???如果不存在 "%ProgramFiles(x86)%" 转到???

---------->[SOLUTION]<---------- ---------->[解决方案]<----------

@echo off @回声关闭

:Restart start "Edit Text" /wait "C:\\Program Files (x86)\\Winstep\\WorkShelf.exe" :Restart start "Edit Text" /wait "C:\\Program Files (x86)\\Winstep\\WorkShelf.exe"

goto restart转到重新启动

adding workshelf.exe instead of winstep made it stable, winstep exe launches a) next start and b) workshelf, only needed workshelf to restart but i can add multiple directory's of thing's to restart if closed.添加 workshelf.exe 而不是 winstep 使其稳定,winstep exe 启动 a) 下一次启动和 b) 工作架,只需要重新启动工作架,但如果关闭,我可以添加多个目录以重新启动。

A simple solution would be一个简单的解决方案是

@echo off
:Restart
start "Winstep Xtreme" /wait "C:\Program Files (x86)\Winstep\WinstepXtreme.exe"
goto Restart

The batch file remains open now all the time.批处理文件现在一直保持打开状态。 The execution of the batch file is halted as long as Winstep Xtreme is running.只要Winstep Xtreme正在运行,批处理文件的执行就会停止。 Once this application terminates itself, the execution of the batch file continues with starting Winstep Xtreme again.一旦此应用程序自行终止,批处理文件的执行将继续并再次启动Winstep Xtreme

You hopefully do not use this batch file just to workaround the limitations of the trial version of Winstep Xtreme .您希望不要使用此批处理文件来解决Winstep Xtreme试用版的限制

Adjust instances of cmd.exe to the .exe you wish to monitor and restart将 cmd.exe 的实例调整为您要监控的 .exe 并重新启动

@ECHO OFF

(
ECHO Set objWMIService = GetObject ("winmgmts:"^)
ECHO Set proc = objWMIService.ExecQuery("select * from Win32_Process Where Name='cmd.exe'"^)
ECHO DO while proc.count ^> 0
ECHO Set proc = objWMIService.ExecQuery("select * from Win32_Process Where Name='cmd.exe'"^)
ECHO if proc.count ^< 1 then exit do
ECHO wscript.sleep 5000
ECHO loop
ECHO Set WshShell=createobject("wscript.shell"^)
ECHO WshShell.run "cmd.exe", 1, true
ECHO WshShell.run "%~0", 0, false
) >%TEMP%\MonitorEXE.vbs

Start %TEMP%\MonitorEXE.vbs

mshta vbscript:msgbox("cmd.exe launched. Monitoring active.",0,"Notification")(window.close)

exit

This Batch will create a vbs script that monitors the process, and restarts it Automatically If no instances of the process exist.此 Batch 将创建一个监控进程的 vbs 脚本,并在不存在进程实例时自动重新启动它。 In the event it has to restart a procces, A message Box is created to Notify that has occured, Then the batch file is called to restart the vbs Montior.如果必须重新启动过程,则会创建一个消息框以通知发生的情况,然后调用批处理文件以重新启动 vbs Montior。

The batch runs Silently (After it's inital launch), and is closed until needed again to restart the .exe Only the notifications will appear.批处理静默运行(在初始启动后),并关闭直到再次需要重新启动 .exe 仅显示通知。

The 'Sleep' in the monitoring vbs is Highly advised, as it dramatically reduces the CPU usage involved in calling the WMI Provider Host强烈建议使用监控 vbs 中的“睡眠”,因为它可以显着降低调用 WMI 提供程序主机时所涉及的 CPU 使用率

A simple solution would be like below that kill your application and start again.一个简单的解决方案如下所示,可以终止您的应用程序并重新启动。

@taskkill /f /im WinstepXtreme.exe >nul
@timeout /t 3 /nobreak >nul
@start C:\"Program Files (x86)"\Winstep\WinstepXtreme.exe >nul
@echo restart complete

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

相关问题 批处理文件,用于检查连接并在没有互联网连接时重新启动程序 - Batch file that checks connectivity and restarts a program when not having internet connection 如何在我启动一个程序的地方编写批处理文件,当该程序完成或关闭时,启动另一个程序 - How Can I Write a Batch File Where I Start one Program and When that Program Finishes or Closes, Start Another 如何启动在我的应用程序关闭后运行的批处理文件 - how to start a batch file that runs after my application closes 批处理文件菜单程序意外关闭 - Batch file Menu program closes unexpectedly 尝试制作批处理文件以通过netstat检测未经授权的连接 - Trying to make a Batch File that detects unauthorized connections via netstat 打开程序的批处理文件等待5秒钟然后关闭该程序 - batch file that opens a program waits 5 seconds then closes that program 双击时批处理文件闪烁并关闭 - Batch file flashes and closes when double Clicked 在另一个特定应用程序启动后关闭1个应用程序的批处理文件 - Batch file that closes 1 application after another specific application has started 如何创建Windows应用程序:检测当前程序并发送快捷方式 - How to create a Windows application that: detects current program and sends shortcuts 专用服务器使用批处理文件重新启动 - Dedicated Server restarts with a batch file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM