Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I tell if an application is already running. 1

Status
Not open for further replies.

rarubio1

Programmer
Jan 25, 2002
64
US
I have written a VB app (app1) that works both as a stand alone app but may also be executed from within another app (app2) via the Shell command. When app2 is running, updates made to app1 need to refresh the information displayed on app2.
How can my app1 know if app2 is running or not?

Thanks,
RR :)
 
app.PrevInstance



Two strings walk into a bar. The first string says to the bartender: 'Bartender, I'll have a beer. u.5n$x5t?*&4ru!2[sACC~ErJ'. The second string says: 'Pardon my friend, he isn't NULL terminated'.
 
I guess I did not state my question correctly. App2 is not an instance of App1. App2 is a totally different app and therefore App.PrevInstance will not work.

Thanks,
RR :)
 
Might not be the most elegant way to do it but App2 could write a boolean value to say a text file when it opens/close and app1 could check for it??

Harleyquinn

---------------------------------
For tsunami relief donations
 
Depending on the OS, you could adapt the code found in Thread222-63270 or use WMI. Since you haven't specified the target OS I can not give a specific solution for you.
 
You can use the return value of the Shell function (Process Id) to check if the other application is running or it has been finished.

See the following code. The function IsProcessRunning does exactly this by querying the process information.

Start a new project, place two command buttons on the form and insert the following code.
___
[tt]
Option Explicit
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
Const PROCESS_QUERY_INFORMATION = &H400
Const STATUS_PENDING = &H103
Dim ProcessId As Long
Private Sub Form_Load()
Command1.Caption = "Start Calculator"
Command2.Caption = "Check Calculator"

Command1.Move 0, 0, 2000, 400
Command2.Move 0, 500, 2000, 400
Command2.Enabled = False
End Sub
Private Sub Command1_Click()
ProcessId = Shell("calc.exe", vbNormalFocus)
Command1.Enabled = False
Command2.Enabled = True
End Sub
Private Sub Command2_Click()
If IsProcessRunning(ProcessId) Then
MsgBox "Calculator is still running."
Else
MsgBox "Calculator has been finished."
Command1.Enabled = True
Command2.Enabled = False
End If
End Sub
Function IsProcessRunning(ProcessId As Long) As Boolean
Dim hProcess As Long, ExitCode As Long
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, 0, ProcessId)
GetExitCodeProcess hProcess, ExitCode
CloseHandle hProcess
IsProcessRunning = ExitCode = STATUS_PENDING
End Function[/tt]
___

Now run the program and test it. The program tells if the shelled application is still running or finished.
 
Thank you all.
Hypetia, the code you provided is exactly what I was looking for, THANK YOU!!!!!!!!

RR :)
 
For future reference see Thread222-63270

LF

"As far as the laws of mathematics refer to reality, they are not certain; as far as they are certain, they do not refer to reality."--Albert Einstein
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top