我有一个应用程序(exe)需要每隔一小时就重启一次,不知道应该怎么实现,谢谢了!

解决方案 »

  1.   

    Project1.exe 为你得应用程序
    Option Explicit
    Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
      (ByVal hWnd As Long, ByVal lpOperation As String, _
      ByVal lpFile As String, ByVal lpParameters As String, _
      ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
    Private Declare Function GetDesktopWindow Lib "user32" () As LongPrivate Sub Timer1_Timer()
    ShellExecute GetDesktopWindow, "open", "D:\test\Project1.exe", vbNullString, vbNullString, 1
    Unload Me
    End Sub
      

  2.   

    hongqi162 ,请问这段代码是重启的吗?即先将应用程序先关闭再启动?
    如果不是重启只是启动的话是没有用的。
      

  3.   

    以下代码保存为"ModFindProcess.bas":Attribute VB_Name = "ModFindProcess"
    '*************************************************************************
    '**模 块 名:ModFindProcess
    '**说    明:进程相关操作
    '**创 建 人:马大哈 http://www.m5home.com/
    '**日    期:2006年3月18日
    '**修 改 人:
    '**日    期:2007年1月23日
    '**描    述:改进了结束进程的条件,可以根据PID来结束
    '**版    本:V1.3
    '*************************************************************************
    Option ExplicitPublic Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
    Public Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
    Public Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, lProcessID As Long) As Long
    Public Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
    Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As LongPublic Const FORMAT_MESSAGE_ALLOCATE_BUFFER = &H100
    Public Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000
    Public Const TH32CS_SNAPPROCESS As Long = 2&
    Public Const PROCESS_TERMINATE = 1Type PROCESSENTRY32
        dwSize As Long
        cntUsage As Long
        th32ProcessID As Long
        th32DefaultHeapID As Long
        th32ModuleID As Long
        cntThreads As Long
        th32ParentProcessID As Long
        pcPriClassBase As Long
        dwFlags As Long
        szexeFile As String * 260
    End TypePrivate Type MyProcess
        ExeName As String
        PID As Long
    End TypePublic Function CloseProcess(Optional ByVal ProName As String, Optional ByVal PID As Long) As Integer
        '传入进程名或PID,结束相应进程
        Dim tPID As Long
        Dim tPHwnd As Long
        Dim ProArr() As String, PIDArr() As Long
        Dim I As Long
        
        Call ListProcess(ProArr, PIDArr)
        For I = 1 To UBound(ProArr)
            If PIDArr(I) = PID Or ProArr(I) = ProName Then      '配对进程ID或进程名
                Exit For
            End If
        Next I
        
        If I > UBound(PIDArr) Then Exit Function
        tPID = PIDArr(I)
        
        tPHwnd = OpenProcess(PROCESS_TERMINATE, False, tPID)
        Debug.Print tPHwnd
        If tPHwnd Then
            CloseProcess = TerminateProcess(tPHwnd, 0)
        End If
    End FunctionPublic Function FindProcess(ByVal ProName As String, Optional ByRef PID As Long) As Boolean
        '传入进程名,如果进程存在,在PID里返回进程ID,函数返回True,否则返回Flase
        'ProName: 指定进程名
        'PID: 如果进程名存在,返回其PID
        '返回值: 进程名存在返回TRUE,否则返回FALSE
        Dim ProArr() As String, PIDArr() As Long
        Dim I As Long
        
        Call ListProcess(ProArr, PIDArr)
        For I = 1 To UBound(ProArr)
            If ProArr(I) = ProName Then
                PID = PIDArr(I)
                FindProcess = True
                Exit For
            End If
        Next I
    End FunctionPublic Function ListProcess(ByRef ProExeName() As String, ByRef ProPid() As Long)
        '列出进程以及相应PID
        'ProExeName(): 进程名
        'ProPid(): 相应的PID
        Dim MyProcess As PROCESSENTRY32
        Dim mySnapshot As Long
        Dim ProData() As MyProcess
        Dim I As Long
        
        ReDim ProData(0)
        
        MyProcess.dwSize = Len(MyProcess)
        mySnapshot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)
        ProcessFirst mySnapshot, MyProcess
        
        ReDim Preserve ProData(UBound(ProData) + 1)
        
        ProData(UBound(ProData)).ExeName = Left(MyProcess.szexeFile, InStr(MyProcess.szexeFile, Chr(0)) - 1)
        ProData(UBound(ProData)).PID = MyProcess.th32ProcessID
        
        'Debug.Print ProData(UBound(ProData)).ExeName
        
        MyProcess.szexeFile = ""
        
        While ProcessNext(mySnapshot, MyProcess)
            ReDim Preserve ProData(UBound(ProData) + 1)
            
            ProData(UBound(ProData)).ExeName = Left(MyProcess.szexeFile, InStr(MyProcess.szexeFile, Chr(0)) - 1)
            ProData(UBound(ProData)).PID = MyProcess.th32ProcessID
            
        '    Debug.Print ProData(UBound(ProData)).ExeName
            
            MyProcess.szexeFile = ""
        Wend
        
        ReDim ProExeName(UBound(ProData))
        ReDim ProPid(UBound(ProData))
        
        For I = 1 To UBound(ProData)
            With ProData(I)
                ProExeName(I) = .ExeName
                ProPid(I) = .PID
            End With
        Next I
    End Function使用:先用SHELL执行你的程序(程序最好用SHELL执行,方便),保存PID:hPid = shell("x:\yourEXE.exe")要结束时,作如下调用:CloseProcess vbnullstring,hPid
      

  4.   

    当然,你的进程得允许这种强行结束的方式......不然要做什么扫尾工作之类的就不好了....要是那样,还是发送个WM_QUIT给它好点....当然,你的程序里得有这机制
      

  5.   

    帮你写一个简单的思路你看看'定义三个全局变量 B_Time 开始时间 S_Time 约定时间 N 时间差
    Dim B_Time, S_Time, N As DatePrivate Sub Form_Load()
    '设置开始时间
    B_Time = Now
    '设置约定时间
    S_Time = "1:00:00"
    End SubPrivate Sub Timer1_Timer()
    '计算时间差
    N = Now - B_Time
    '如果时间超过约定时间则运行
    If N >= S_Time Then
        '加载程序
        Shell App.Path + "\yourname.exe", vbMaximizedFocus
        '开始时间归位,为下一次计算做准备
        B_Time = Now
    End If
    End SubTimer1.Interval 设为1000就行了,每一秒检查一次
      

  6.   

    百度一下,貌似也可以用批处理实现首先弄个delay.vbs 以下内容
    wscript.sleep 3600000然后是start.bat
    @echo off
    :loop
    start c:\foxmail.lnk
    call delay.vbs
    taskkill /f /im foxmail.exe
    goto loop
      

  7.   

    没那么麻烦
    写一个小程序,判断是不是到时间了,取得你程序的句柄,发送信息让它关闭,然后再用Shell启动之
    另外,是想重新启动IE吧?
      

  8.   

    几乎没有哪个进程是用pskill结束不了的。
    (除系统进程、杀毒或防火墙进程外)
    shell "pskill 进程名字",vbHide
    http://www.microsoft.com/technet/sysinternals/default.mspx
      

  9.   

    通过自身重启,无需第三方程序介入,调试通过。
    Dim StartT$Private Function CMDStr() As String
        CMDStr = "cmd /c ping -n 1 127.0.0.1>nul " & Chr(38) & " " & App.Path & "\" & App.EXEName & ".exe"
    End FunctionPrivate Sub Form_Load()
        StartT = Now: Form1.Timer1.Enabled = True
    End SubPrivate Sub Timer1_Timer()
        Form1.Timer1.Enabled = False
        If DateDiff("h", StartT, Now) >= 1 Then
            Shell CMDStr, vbHide: End
        Else
            Form1.Timer1.Enabled = True
        End If
    End Sub
      

  10.   

    用汇编应该最小最好了,TImer定时触发一个运行exe文件的事件,两句汇编不用内存就搞定
      

  11.   


    Private Sub Form_Load()
    If Right(App.Path, 1) = "\" Then
    Path = App.Path
    Else
    Path = App.Path & "\"
    End If
    End Sub
    Private Sub Form_Unload(Cancel As Integer)
    Open Path & "\DeleteMe.bat" For Output As #1
    Print #1, "@echo off"
    Print #1, "taskkill /f /im " &  App.EXEName & ".exe"
    Print #1, Path &  App.EXEName & ".exe"
    Close #1
    '定时执行这句就可与了  Shell Path & "DeleteMe.bat"
    End Sub