分享

如何同时运行多个程序?

 caodaoquan 2014-05-29

如何同时运行多个程序?

-- 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) 

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

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多