您的位置:首页 > 其它

并发执行多个程序的方法

2016-09-02 16:42 399 查看
如何同时运行多个程序?

-- by benbenknight

   

    同时运行多个程序一般有两种方式,一种是串行的,即运行完一个程序再运行下一个;一种是并行的,即程序运行后不必等待执行结束就运行下一个。

    最简单的方法就是使用批处理文件或Windows脚本,也可以使用VC写个小程序,读取配置文件中的程序路径及参数,循环调用CreateProcess函数即可。下面主要介绍前两种方法。

    一、批处理文件

    批处理命令中,call和直接输入程序路径采用等待程序结束方式,start采用的是不等待的方式。下面几个小例子中,其中RunS1.bat、RunS2.bat为串行运行,RunP.bat为并行运行。

---------------------------------

文件RunS1.bat:

echo off

rem RunS1.bat

rem 同时串行运行多个程序

set cmd1=D:test1

set cmd2=D:test2

call %cmd1%

call %cmd2%

---------------------------------

文件RunS2.bat:

echo off

rem RunS2.bat

rem 同时串行运行多个程序

set cmd1=D:test1

set cmd2=D:test2

%cmd1%

%cmd2%

---------------------------------

文件RunP.bat:

echo off

rem RunP.bat

rem 同时并行运行多个程序

set cmd1=D:test1

set cmd2=D:test2

start %cmd1%

start %cmd2%

---------------------------------

 

    二、Windows脚本

    WshShell对象的Run方法即可采用等待程序结束的方式,也可采用不等待的方式;Exec方法采用不等待的方式。下面几个小例子中,

---------------------------------

文件RunS.vbs:

' RunS.vbs

' 同时串行运行多个程序

strCmd1 = "D:\test1.exe"

strCmd2 = "D:\test2.exe"

Set wshShell = CreateObject("WScript.Shell")

nWindowStyle = 1 ' 激活并显示窗口

bWaitOnReturn = 1 ' 等待程序执行完毕再执行下一条语句

wshShell.Run strCmd1,nWindowStyle,bWaitOnReturn

wshShell.Run strCmd2,nWindowStyle,bWaitOnReturn

---------------------------------

文件RunP1.vbs:

' RunP1.vbs

' 同时并行运行多个程序

strCmd1 = "D:\test1.exe"

strCmd2 = "D:\test2.exe"

Set wshShell = CreateObject("WScript.Shell")

nWindowStyle = 1 ' 激活并显示窗口

bWaitOnReturn = 0 ' 不必等待程序执行完毕再执行下一条语句

wshShell.Run strCmd1,nWindowStyle,bWaitOnReturn

wshShell.Run strCmd2,nWindowStyle,bWaitOnReturn

---------------------------------

文件RunP2.vbs:

' RunP2.vbs

' 同时并行运行多个程序

strCmd1 = "D:\test1.exe"

strCmd2 = "D:\test2.exe"

Set wshShell = CreateObject("WScript.Shell")

wshShell.Exec(strCmd1)

wshShell.Exec(strCmd2) 

---------------------------------

等待并行程序结束:

:loop

ping -n 2 127.1>nul

tasklist /fi "windowtitle eq 并行*" |findstr "cmd.exe">nul&&(goto loop)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐