VB怎样判断、防止程序重复执行

来源:互联网 发布:每周原油数据 编辑:程序博客网 时间:2024/04/29 16:58

Private Sub Form_load()
    '判断程序是否已经运行
    If App.PrevInstance Then
     MsgBox "本程序已经运行!", vbInformation Or vbOKOnly, "提示信息"
     Unload Me
     Exit Sub
    End If
    '以下是主要程序
    ' ……
    End Sub

    附:另一个例子:
    Option Explicit
    Public Sub CheckExist(fm As Form) '防止程序重复执行
      Dim title As String
      If App.PrevInstance Then
      title = App.title
      Call MsgBox("这程序已执行", vbCritical)
      App.title = "" '如此才不会 Avtivate 到自己
      fm.Caption = ""
      AppActivate title 'activate 先前就已运行的程序
      End ' 结束
      End If
    End Sub
    Private Sub Form_Load()
     Call CheckExist(Me)
    End Sub

原创粉丝点击