并发执行多个程序的方法
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)
-- 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)
相关文章推荐
- 程序不能并发执行,为使多个程序并发执行,引入进程
- 操作系统实验参考以上示例程序中建立并发进程的方法,编写一个多进程并发执行程序。父进 程首先创建一个执行ls命令的子进程然后再创建一个执行ps命令的子进程,并控制 ps 命令总在 ls 命令之前执行。
- 程序不能并发执行,为使多个程序并发执行,引入进程
- 程序不能并发执行,为使多个程序并发执行,引入进程
- 执行方法为PL/SQL Stored Procedure的并发程序
- 程序不能并发执行,为使多个程序并发执行,引入进程
- 程序不能并发执行,为使多个程序并发执行,引入进程
- C#中得到程序当前工作目录和执行目录的一些方法
- ora-12154 tns 无法处理服务名(执行的程序途径不一致导致,枚举网络数种方法,大家还有其他解决办法?)
- Linux操作系统下 MONO执行C#程序的方法
- 正试图在 os 加载程序锁内执行托管代码。不要尝试在 DllMain 或映像初始化函数内运行托管代码,这样做会导致应用程序挂起。问题的解决方法!
- 总结C#中得到程序当前工作目录和执行目录的一些方法
- C与Java中计算程序执行时间的方法
- 总结C#中得到程序当前工作目录和执行目录的一些方法
- C#中得到程序当前工作目录和执行目录的一些方法
- [转]C#中得到程序当前工作目录和执行目录的一些方法
- Linux操作系统下 MONO执行C#程序的方法
- 总结C#中得到程序当前工作目录和执行目录的一些方法
- C#中得到程序当前工作目录和执行目录的一些方法
- WinForm程序如何等待多个线程执行完后提示用户?