检查shell程序是否已打开

时间:2010-01-05 14:42:01

标签: .net vb.net ms-access

我正在使用Tony Toews自动FE程序启动Access ADE。如果有更新的版本,AutoFE程序将从服务器复制最新版本的ADE,然后启动它。 我的代码如下:

Dim stAutoFE As String = "V:\Apps\autofe\startmdb.exe /cmd /inifile: " & """"            & "V:\Apps\AutoFE\SSP.ini" & """"
Shell(stAutoFE, AppWinStyle.Hide, True)
System.Threading.Thread.Sleep(1000) ' Time delay


Dim oAccess As Access.Application
oAccess = GetObject(SSP_Path)  ' The ADE file location

我必须将Sleep延迟放入,否则GetObject将再次打开ADE。 但我不知道服务器的副本需要多长时间,因此我需要删除该Sleep行并检查ADE是否已打开。 我怎样才能做到这一点? 谢谢 Diarmuid

2 个答案:

答案 0 :(得分:1)

这是我最终需要的。我只是检查Windows标题的左侧是否与我要查找的名称相匹配。 用法是:

   *If IsAppOpen("OMain", "SSP") then*

OMain是Access类名。

代码如下:

    Private Declare Auto Function FindWindow Lib "user32" ( _
       ByVal lpClassName As String, _
       ByVal lpWindowName As String) As IntPtr

   Private Declare Auto Function GetWindowText Lib "user32" ( _
      ByVal hwnd As IntPtr, _
      ByVal lpString As String, _
      ByVal cch As Integer) As Integer

   Public Function IsAppOpen(ByVal stClassName As String, ByVal stAppName As String) As Boolean
      ' Find out if application is open
      ' Checks if stAppName matches the left hand characters of the Window name

      Dim hwnd As IntPtr = FindWindow(stClassName, vbNullString)

      Dim stWindowText As String
      Dim bAppFound As Boolean = False

      If Not hwnd.Equals(IntPtr.Zero) Then

         stWindowText = New String(Chr(0), 100)
         Dim intLength As Integer = GetWindowText(hwnd, stWindowText, stWindowText.Length)

         If (intLength <= 0) OrElse (intLength > stWindowText.Length) Then
            bAppFound = False
         Else
            Dim stTitle As String = stWindowText.Substring(0, intLength)

            If stTitle.Substring(0, stAppName.Length) = stAppName Then
               bAppFound = True
            End If
         End If
      End If

      IsAppOpen = bAppFound

   End Function

答案 1 :(得分:0)

您应该放弃使用Shell并开始使用Process.WaitForExit method,这样您就不需要使用Thread.Sleep;

您可以在此处查看一些相关示例代码:Process.ExitCode Property

编辑:作为C#:

Process process = Process.Start(
    new ProcessStartInfo(@"V:\Apps\autofe\startmdb.exe", 
        String.Format(@"/cmd /inifile: ""{0}""", @"V:\Apps\AutoFE\SSP.ini")));
process.WaitForExit();