VB判断一个程序是否运行。如果不运行5分钟后关机问题
我想用VB做一个程序,自动判断程序CrossFire.exe是否在运行,如果不在运行5分钟后自动关机。求代码。我是新手请说明原理...
我想用VB做一个程序,自动判断程序CrossFire.exe是否在运行,如果不在运行5分钟后自动关机。求代码。我是新手请说明原理
展开
2个回答
展开全部
option Explicit
dim wmi,proc,procs,proname,flag,WshShell
Do
proname="CrossFire.exe" '输入要检测的进程名称
set wmi=getobject("winmgmts:{impersonationlevel=impersonate}!\\.\root\cimv2")
set procs=wmi.execquery("select * from win32_process")
flag=true
for each proc in procs
if strcomp(proc.name,proname)=0 then
flag=false
exit for
end if
next
set wmi=nothing
if flag then
Set WshShell = Wscript.CreateObject("Wscript.Shell")
WshShell.Run ("C:\windows\System32\shutdown.exe /s /t 300") '执行shutdown命令 使用/s /t 来自定关机时间
end if
wscript.sleep 50000 '检测间隔时间,这里是50秒
loop
dim wmi,proc,procs,proname,flag,WshShell
Do
proname="CrossFire.exe" '输入要检测的进程名称
set wmi=getobject("winmgmts:{impersonationlevel=impersonate}!\\.\root\cimv2")
set procs=wmi.execquery("select * from win32_process")
flag=true
for each proc in procs
if strcomp(proc.name,proname)=0 then
flag=false
exit for
end if
next
set wmi=nothing
if flag then
Set WshShell = Wscript.CreateObject("Wscript.Shell")
WshShell.Run ("C:\windows\System32\shutdown.exe /s /t 300") '执行shutdown命令 使用/s /t 来自定关机时间
end if
wscript.sleep 50000 '检测间隔时间,这里是50秒
loop
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
我拿任务管理器做实例
你在窗体中创建个timer 和两个cmmandbutton 把下面的代码复制进去直接可以运行
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Sub Command1_Click()
Shell "cmd /c call taskmgr", vbHide
End Sub
Private Sub Command2_Click()
Shell "cmd /c taskkill /im taskmgr.exe", vbHide
End Sub
Private Sub Form_Load()
Timer1.Interval = 100
Command1.Caption = "启动"
Command2.Caption = "关闭"
End Sub
Private Sub Timer1_Timer()
Cls
Dim hwnd As Long
hwnd = FindWindow(vbNullString, "windows 任务管理器") ' 取得进程标识符
If hwnd = 0 Then
Print "任务管理器未运行"
Else
Print "任务管理器已运行"
End If
End Sub
你在窗体中创建个timer 和两个cmmandbutton 把下面的代码复制进去直接可以运行
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Sub Command1_Click()
Shell "cmd /c call taskmgr", vbHide
End Sub
Private Sub Command2_Click()
Shell "cmd /c taskkill /im taskmgr.exe", vbHide
End Sub
Private Sub Form_Load()
Timer1.Interval = 100
Command1.Caption = "启动"
Command2.Caption = "关闭"
End Sub
Private Sub Timer1_Timer()
Cls
Dim hwnd As Long
hwnd = FindWindow(vbNullString, "windows 任务管理器") ' 取得进程标识符
If hwnd = 0 Then
Print "任务管理器未运行"
Else
Print "任务管理器已运行"
End If
End Sub
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询