• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 247
  • Last Modified:

How to run a another application in sync.

Hi all,
    I'm looking for a way to run a shell command (Linking to another .exe) in sync (i.e. my program will will not continue to execute until the other application ends).

    How can I also detect if my application (NOT the one I'm calling) is already running (since I only want one instance of it running ever)

1 Solution
Here is a link to code that will shell and wait


At the beginning of your app this code will check for a previous instance

if app.PrevInstance then
  msgbox "Already running"
end if
Here is also a link to an EE PAQ with code that will execute and wait.  It may be a better way to go.

declare the following function in a module:

Public 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

to execute any file use the following syntax from your project:
ShellExecute 0, "Open", "application_name.exe", "", "", vbNormalFocus

now to detect if your application is already running use the following piece of code in your sub main or startup form:

if App.PrevInstance then
  'another instance of this application is already running
  msgbox "application already running"
 'this is the only instance of this application
end if
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.


I'm not sure if you read the entire question.  The code you provided will execute an file, but it will not wait on that process to end before continuing.  

If you noticed the PrevInstance code you posted was already posted above.  Why duplicate comments?  Post if you have a different or an alternate way to do something, but not if you have identical code.

Function MyFunc()
  Dim myObject As Object
  Dim RetVal As Variant
  Set myObject = CreateObject("WScript.Shell")
  RetVal = myObject.Run("c:\windows\notepad.exe", 1, True)
  MsgBox "Notepad is closed"

End Function

'TRue - wait until proc is over
'False don't wait
DragonWolfAuthor Commented:
How can I lock/end this question? I already figure this out.
You'll need to post a question to community support stating you would like to delete the question.  Give the question id as well as why you would like it deleted.
I am curious how exactly you figured this out?  The duplicate question you posted did not answer this question, but it answered another question you posted about DOS full screen problems.
The duplicate question also did not answer this question fully, but only one small portion of the question.  If you figured out the answer yourself then this question can be deleted, but if any of the comments that were made were helpful in helping you solve the problem then points need to be awarded.
DragonWolfAuthor Commented:
I solved the sinking problem myself.

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now