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)

    Thanks
LVL 1
DragonWolfAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
gencrossConnect With a Mentor Commented:
Here is a link to code that will shell and wait

http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=6071&lngWId=1

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

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

http://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=20270080
0
 
selim007Commented:
dear,
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"
else
 'this is the only instance of this application
end if
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
gencrossCommented:
selim007,

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.

0
 
iboutchkineCommented:
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
0
 
DragonWolfAuthor Commented:
How can I lock/end this question? I already figure this out.
0
 
gencrossCommented:
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.
0
 
gencrossCommented:
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.
0
 
gencrossCommented:
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.
0
 
DragonWolfAuthor Commented:
I solved the sinking problem myself.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.