为什么GUI应用程序块的批处理文件? [英] Why GUI application blocks a batch file?

查看:168
本文介绍了为什么GUI应用程序块的批处理文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有在互联网上声称的GUI和控制台应用程序之间的区别之一是,运行批处理文件的GUI应用程序不会阻止其执行多次提到,在运行控制台应用程序不会阻止它。

There are many references on Internet claiming that one of differences between a GUI and a console application is that running the GUI application from a batch file does not block its execution, while running the console application does block it.

很少有多处提到,这些都是特别是SO / SE:

Few of many references, these are particularly from SO/SE:

  • How can I get an MFC application to block from the command line?
  • How to wait for a process to terminate to execute another process in batch
  • How do you wait for an exe to complete in batch file?

此外,我记得这是/是真实的。

Moreover, I myself remember this is/was true.

然而,这似乎并没有这样的工作方式。

Yet it does not seem to work this way.

我已经在一个简单的批处理文件测试此类似:

I've tested this on a simple batch file like:

echo Pre
notepad
echo Post

发表不打印,直到我关闭记事本。为什么当一个记事本显然是一个GUI应用程序?

The Post is not printed until I close notepad. Why, when a notepad is clearly a GUI application?

我测试过这在Windows 8,7和XP只是为了排除一种可能性,即行为在最近版本的Windows中发生了变化。我试图禁用命令扩展尽可能罪魁祸首之一了。

I've tested this on Windows 8, 7, and XP just to rule out a possibility that the behavior has changed in recent versions of Windows. I've tried to disable command extensions as one of possible culprits too.

推荐答案

它有怎么说你启动运行,并终止应用程序执行。有些程序启动另一个进程,然后终止,其他人继续运行。 CALC.EXE和Notepad.exe的只是运行,直到您关闭它们。 WRITE.EXE并启动作为文件关联而产生的任何程序(例如,位图,波形文件,控制面板程序等),实际启动其他程序,然后是发动了会终止控制权返回到批处理文件的过程因此它可以执行下一行

It has to do with how the application that you launch runs and terminates. Some programs launch another process and then terminate, others continue to run. Calc.exe and Notepad.exe simply run until you close them. Write.exe and any program that launches as a result of a file association (e.g., bitmap, wave file, control panel applet, etc.), actually launch another program and then the process that launched them terminates returning control back to the batch file so it can execute the next line.

下面是一些例子:

@echo off

echo Starting Calc.exe
calc.exe
echo Calc was closed by the user

echo Starting Notepad.exe
Notepad.exe
echo Notepad was closed by the user

echo Starting WordPad.exe
write.exe
echo Write launched WordPad and then terminated allowing the batchfile to continue

echo Starting Services.msc
services.msc
echo Windows launched MMC, opened services.msc, then returned control to the batchfile

echo Launching WMP via Chord.wav
c:\windows\media\chord.wav
echo Windows launched WMP, opened Chord.wav, then returned control to the batchfile

在CMD进程知道计算器记事本还在运行,因为它催生了他们自己。在CMD过程做的的知道,别人还在运行,因为中间过程终止。

The CMD process knows Calc and Notepad are still running because it spawned them itself. The CMD process does not know that the others are still running because the intermediate process terminated.

要注意这一点,打开的Process Explorer 并查看层次结构树中显示的过程。 CALC.EXE和Notepad.exe的都保持为CMD进程运行的批处理文件的子进程。 WRITE.EXE和程序mmc.exe(Services.msc)中都成为顶级工艺,不再带孩子到CMD进程。 WMPlayer.exe仍然是一个子进程SVCHOST.EXE,这是Windows的启动它。该CMD过程中不知道他们仍然在运行,因为没有发动他们,一些其他Windows程序一样。所以,继续执行...

To observe this, open Process Explorer and view the processes displayed in the hierarchical tree. Calc.exe and Notepad.exe both remain as child processes of the CMD process that ran the batchfile. Write.exe and MMC.exe (services.msc) both become top-level processes, no longer children to the CMD process. WMPlayer.exe remains a child process to svchost.exe, which is how Windows launched it. The CMD process doesn't know they are still running because it didn't launch them, some other Windows process did. So execution continues...

这还有一个例子程序为MSPaint.exe怎样的功能。如果使用内置的骨形成蛋白文件关联的Windows上运行它,然后Windows启动MSPaint.exe和控制立即返回到批处理文件。但是,如果你通过BMP到MSPaint.exe,那么批处理文件等待您继续之前关闭MSPAINT。 (我是一个dev的机器,没有骨形成蛋白,因此创建一个简单的一个叫做C:\\ MyBitmap.bmp)

One more example of this is how MSPaint.exe functions. If you run it by using the Windows built-in file association for BMPs, then Windows launches MSPaint.exe and control is immediately returned to the batchfile. However, if you pass the BMP to MSPaint.exe, then the batchfile waits for you to close MSPaint before continuing. (I'm on a dev machine with no BMPs, so create a simple one called C:\MyBitmap.bmp.)

@echo off
C:\MyBitmap.bmp
calc.exe
mspaint.exe C:\MyBitmap.bmp
notepad.exe

CALC.EXE将立即打开,Notepad.exe的会不开,直到关闭MSPaint.exe的第二个实例。

Calc.exe will open immediately, Notepad.exe will not open until you close the second instance of MSPaint.exe.

我知道你没有问关于通过文件关联启动Windows的过程,但它只是表明了拥有进程如何能够发生变革。如果CMD进程拥有的推出过程中,应该等到它终止继续执行。如果产生的进程,手控关到另一个进程,那么CMD过程中不知道孙儿过程,它与它的继续执行上。

I know you didn't ask about launching Windows processes via their file association, but it just demonstrates how the owning process can change. If the CMD process owns the launched process, it should wait until it terminates to continue execution. If the spawned process hands control off to another process, then the CMD process doesn't know about the grandchild process and it continues on with its execution.

这篇关于为什么GUI应用程序块的批处理文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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