?
Solved

VB check if another program is running

Posted on 2013-02-05
12
Medium Priority
?
355 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 2000 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

Question has a verified solution.

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

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

765 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