如何用VB延时执行命令?
要求功能如下:同一目录下有三个文件:file1.exe、file2.exe、file3.exe当我运行file1.exe之后二十秒后自动执行file2.exe,再过十秒执...
要求功能如下:同一目录下有三个文件:file1.exe、file2.exe、file3.exe
当我运行file1.exe之后二十秒后自动执行file2.exe,再过十秒执行file3.exe,再过五秒结束file2.exe的进程并删除file2.exe,再过五秒删除自身也就是file1.exe
这样的代码应该怎么写啊?请写全,非常感谢如果成功了再增加20分
用for循环来进行的话很费内存,加一个timer控件的话它就不延时而是每隔一会就执行一次,而且一开始就执行,有没有什么好的办法啊? 展开
当我运行file1.exe之后二十秒后自动执行file2.exe,再过十秒执行file3.exe,再过五秒结束file2.exe的进程并删除file2.exe,再过五秒删除自身也就是file1.exe
这样的代码应该怎么写啊?请写全,非常感谢如果成功了再增加20分
用for循环来进行的话很费内存,加一个timer控件的话它就不延时而是每隔一会就执行一次,而且一开始就执行,有没有什么好的办法啊? 展开
4个回答
展开全部
Dim i As String
Private Sub Command1_Click()
Print "运行文件1"
Shell "文件路径\file1.exe"
Timer1.Interval = 20000
Timer1.Enabled = True
End Sub
Private Sub Form_Load()
i = 0
End Sub
Private Sub Timer1_Timer()
i = i + 1
Select Case i
Case 1
Print "运行文件2"
Shell "文件路径\file2.exe"
Timer1.Interval = 10000
Case 2
Print "运行文件3"
Shell "文件路径\file3.exe"
Timer1.Interval = 5000
Case 3
Print "结束文件2并删除"
Shell "cmd /c taskkill /f /im file2.exe"
Kill "文件路径\file2.exe"
Timer1.Interval = 10000
Case 4
Print "结束文件1并删除"
Shell "cmd /c taskkill /f /im file1.exe"
Kill "文件路径\file1.exe"
Timer1.Interval = 0
Timer1.Enabled = False
End Select
End Sub
Private Sub Command1_Click()
Print "运行文件1"
Shell "文件路径\file1.exe"
Timer1.Interval = 20000
Timer1.Enabled = True
End Sub
Private Sub Form_Load()
i = 0
End Sub
Private Sub Timer1_Timer()
i = i + 1
Select Case i
Case 1
Print "运行文件2"
Shell "文件路径\file2.exe"
Timer1.Interval = 10000
Case 2
Print "运行文件3"
Shell "文件路径\file3.exe"
Timer1.Interval = 5000
Case 3
Print "结束文件2并删除"
Shell "cmd /c taskkill /f /im file2.exe"
Kill "文件路径\file2.exe"
Timer1.Interval = 10000
Case 4
Print "结束文件1并删除"
Shell "cmd /c taskkill /f /im file1.exe"
Kill "文件路径\file1.exe"
Timer1.Interval = 0
Timer1.Enabled = False
End Select
End Sub
展开全部
实际上用 Timer 挺好的……
在 Form 上放置一个 Interval 为 1000 的 Timer 控件,命名 Timer1,
代码如下:
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Const PROCESS_ALL_ACCESS = &H100000 + &HF0000 + &HFFF
Private Sub Timer1_Timer()
On Error GoTo ErrHandle
Static I As Long, HProcFile2 As Long
I = I + 1
'每执行一次,给计数器加 1,当 Interval 为 1000 时,I - 1 实际上就是秒数
Select Case I - 1
Case 20
HProcFile2 = OpenProcess(PROCESS_ALL_ACCESS, 0, Shell(App.Path & "\file2.exe", vbNormalFocus))
'得到所有权限(包括结束进程)
Case 30
Shell App.Path & "\file3.exe", vbNormalFocus
Case 35
TerminateProcess HProcFile2, -1
'关闭 File3 的进程,退出码为 -1(非正常退出)
CloseHandle HProcFile2
DoEvents
Case 37 '稍后再删除没有什么影响吧
Kill App.Path & "\file2.exe"
Case 40
Kill App.Path & "\file1.exe"
Timer1.Enabled = False
End Select
Exit Sub
ErrHandle:
MsgBox Err.Description, 48
End Sub
在 Form 上放置一个 Interval 为 1000 的 Timer 控件,命名 Timer1,
代码如下:
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Const PROCESS_ALL_ACCESS = &H100000 + &HF0000 + &HFFF
Private Sub Timer1_Timer()
On Error GoTo ErrHandle
Static I As Long, HProcFile2 As Long
I = I + 1
'每执行一次,给计数器加 1,当 Interval 为 1000 时,I - 1 实际上就是秒数
Select Case I - 1
Case 20
HProcFile2 = OpenProcess(PROCESS_ALL_ACCESS, 0, Shell(App.Path & "\file2.exe", vbNormalFocus))
'得到所有权限(包括结束进程)
Case 30
Shell App.Path & "\file3.exe", vbNormalFocus
Case 35
TerminateProcess HProcFile2, -1
'关闭 File3 的进程,退出码为 -1(非正常退出)
CloseHandle HProcFile2
DoEvents
Case 37 '稍后再删除没有什么影响吧
Kill App.Path & "\file2.exe"
Case 40
Kill App.Path & "\file1.exe"
Timer1.Enabled = False
End Select
Exit Sub
ErrHandle:
MsgBox Err.Description, 48
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
帮你写了个函数。跟你说的for循环差不多,应该不会废多少内存的
Public Sub BKWait(HowManySecs)
Dim EndWait
EndWait = DateAdd("s", HowManySecs, Now)
While Now < EndWait
Wend
End Sub
Public Sub BKWait(HowManySecs)
Dim EndWait
EndWait = DateAdd("s", HowManySecs, Now)
While Now < EndWait
Wend
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
使用TIMER控件,代码如下:
file1代码:(加入控件timer1,inerval设定为20000;加入控件timer2,inerval设定为10000,Enabled属性设定为FALSE;加入控件timer3,inerval设定为5000,Enabled属性设定为FALSE)
Private Sub Timer1_Timer()
Shell "file2.exe"
Timer1.Enabled = False
Timer2.Enabled = True
End Sub
Private Sub Timer2_Timer()
Shell "file3.exe"
Timer2.Enabled = False
Timer3.Enabled = True
End Sub
Private Sub Timer3_Timer()
'调用API结束file2的进程,代码略
KIll "file2.exe"
Timer3.Enabled = False
End Sub
file3代码:(加入控件timer1,inerval设定为5000)
Private Sub Timer1_Timer()
'调用API结束file1的进程,代码略
kill "file1.exe"
Timer1.Enabled = False
End Sub
file1代码:(加入控件timer1,inerval设定为20000;加入控件timer2,inerval设定为10000,Enabled属性设定为FALSE;加入控件timer3,inerval设定为5000,Enabled属性设定为FALSE)
Private Sub Timer1_Timer()
Shell "file2.exe"
Timer1.Enabled = False
Timer2.Enabled = True
End Sub
Private Sub Timer2_Timer()
Shell "file3.exe"
Timer2.Enabled = False
Timer3.Enabled = True
End Sub
Private Sub Timer3_Timer()
'调用API结束file2的进程,代码略
KIll "file2.exe"
Timer3.Enabled = False
End Sub
file3代码:(加入控件timer1,inerval设定为5000)
Private Sub Timer1_Timer()
'调用API结束file1的进程,代码略
kill "file1.exe"
Timer1.Enabled = False
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询