views:

60

answers:

2

Hi 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

A: 

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();
Rubens Farias
Thanks. I've tried the following: Dim myProcess As Process = Nothing myProcess = Process.Start(stAutoFE) While Not myProcess.HasExited myProcess.WaitForExit(100) myProcess.Refresh() End While myProcess.Close() But Process.Start fails for me - I get the error message: "The system cannot find the file specified" even though it the same path as I had for the Shell. What should I do differently? Thanks
Awesomoprog
Got it going by trying myProcess = Process.Start(stAutoFE, stParms) but still have the exact same problem as with the Shell command. It could be the 'startmdb.exe' is finished, but the Msaccess.exe isn't.
Awesomoprog
Are you using that `WaitForExit`?
Rubens Farias
Yep. I've even tried Do Process.Refresh Loop While Not myProcess.WaitForExit(100)and its still the same
Awesomoprog
lets start over: do you need start a command and wait until its done, OR to check if some process has started?
Rubens Farias
Well I guess I need to make sure that the Access file, SSP.ADE, has started. It doesn't matter if the command is done. It there a way to check that the ADE is running? That it is in Task Manager applications, or some such. Thanks!
Awesomoprog
When I think about it, and depending on what StartMethod you have chosen in the settings, the Auto FE UPdater itself does a shell to msaccess.exe. the AutoFEUpdater.exe finishes doing it's job within a very short period of time, starts Access and then exits.
Tony Toews
I need to know if MSAccess.exe process is running, and the database is SSP.ADE. Anyone know how VB code for reading the task manager?
Awesomoprog
+1  A: 

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
Awesomoprog