开发者

Check if shell program has opened

开发者 https://www.devze.com 2022-12-15 17:37 出处:网络
I\'m launching an Access ADE, using Tony Toews auto FE program. The AutoFE programs will, if newer version available, copy the latest version of the ADE from the server, then launch it.

I'm launching an Access ADE, using Tony Toews auto FE program. The AutoFE programs will, if newer version available, copy the latest version of the ADE from the server, then launch it. My code looks like:

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

I had to put the Sleep delay in, otherwise the GetObject would open the ADE a second time. But I don't know how long the copy from the server will take, so I need to remove that Sleep line and check that the ADE has opened. How can I do this? Thanks Diarmuid


This is what I need in the end. I just check that the left hand of the Windows title matches the names of what I am looking for. Usage would be:

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

OMain is the Access class name.

Code as follows:

    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


You should drop using Shell and start using Process.WaitForExit method, so you won't need to use a Thread.Sleep;

You can see some related sample code here: Process.ExitCode Property

EDIT: As C#:

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

精彩评论

暂无评论...
验证码 换一张
取 消