Solved

VB check if another program is running

Posted on 2013-02-05
12
354 Views
Last Modified: 2016-02-28
In Visual Basic I want to check if a certain program is running, e.g. C:\MyProgram.exe

(If not then I'll go ahead and launch it, which I already have the code for, but I don't want to launch it if it's already running. I don't want to launch a 2nd copy.)

Code?
0
Comment
Question by:deleyd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +3
12 Comments
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 38855780
Process.GetProcessesByName("MyProgram")

Open in new window

0
 
LVL 42

Expert Comment

by:sedgwick
ID: 38855781
use the application name without the extension (exe).
btw, u need the following namespace to use Process:
using System.Diagnostics;

Open in new window

0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 38855789
therei a function for that
Public Function IsProcessRunning(name As String) As Boolean
	
	For Each clsProcess As Process In Process.GetProcesses()
		If clsProcess.ProcessName.StartsWith(name) Then
			'process found so it's running so return true
			Return True
		End If
	Next
		Return False
End Function

Open in new window

Raise a messagebox end exit the sub if it running...
Hope it helps
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38855792
You would need that line of code into a condition

If Process.GetProcessesByName("MyProgram").Count = 0 Then
    'Launch it
End If
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 38855795
As sedgwick demonstrated, the GetProcessesByName() method expects the filename without a path and without any extension.  Here's  another example:
        Dim FullPath As String = "C:\MyProgram.exe"
        Dim FileName As String = System.IO.Path.GetFileNameWithoutExtension(FullPath)
        If Process.GetProcessesByName(FileName).Length = 0 Then
            Process.Start(FullPath)
        End If

Open in new window

0
 
LVL 42

Expert Comment

by:sedgwick
ID: 38855822
here's the complete code which bring to front the application if already is running:

Private Declare Function ShowWindow Lib "user32" _
(ByVal hWnd As System.IntPtr, ByVal nCmdShow As Long) As Long

Private Const SW_RESTORE = 9


if(Process.GetProcessesByName("MyProgram").Count() == 0){
//launch the program
}else{
ShowWindow(proc.MainWindowHandle, SW_RESTORE)
}

Open in new window

0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 38855826
Too much traffic !! :)
0
 

Expert Comment

by:user1010
ID: 41479546
I tried this code to check if a web browser is running but I got error.

Dim FullPath As String = "C:\Program Files (x86)\Mozilla Firefox\firefox.exe"
        Dim FileName As String = System.IO.Path.GetFileNameWithoutExtension(FullPath)
        If Process.GetProcessesByName(FileName).Length = 0 Then
            Process.Start(FullPath)
        End If

Open in new window


Am I missing some code?
0
 

Author Comment

by:deleyd
ID: 41482061
Was the error, "The system cannot find the file specified"?

Need to first check that file firefox.exe actually exists.
0
 

Expert Comment

by:user1010
ID: 41483382
checkFFox.vbs(1, 14) Microsoft VBScript compilation error: Expected end of statement
0
 

Author Comment

by:deleyd
ID: 41484110
https://msdn.microsoft.com/en-us/library/8dy0ah20.aspx
Can you post lines 13 & 14 of the code?
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 41484668
@user1010, the posted solutions are in VB.Net, which doesn't work in VBScript...
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question