[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

VB check if another program is running

Posted on 2013-02-05
12
Medium Priority
?
356 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:
Meir Rivkin earned 2000 total points
ID: 38855780
Process.GetProcessesByName("MyProgram")

Open in new window

0
 
LVL 42

Expert Comment

by:Meir Rivkin
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
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.

 
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:Meir Rivkin
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

What’s Wrong with Your Cloud Strategy ?

Even as many CIOs are embracing a cloud-first strategy, the reality is that moving to the cloud is a lengthy process and the end-state is likely to be a blend of multiple clouds—public and private. Learn why multicloud solutions matter in this webinar by Nimble Storage.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

650 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