Solved

control if a process is being executed visual basic 2012

Posted on 2014-02-24
8
417 Views
Last Modified: 2014-02-24
Hi Experts,
We need to check if an exe file is already rinnung/working,
 if not then launch it from our App.
Important not to have two instances of the same App at the same time.

Can you help us with some source that can do this in visual basic ?
Regards!
0
Comment
Question by:rguillermo
[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
8 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39882854
Is the exe file something you have coded, in other words can you modify it ?
0
 

Author Comment

by:rguillermo
ID: 39882858
We I think we can modify the source... its a legacy App.. but pretty much used today. Works just fine.
0
 
LVL 21

Assisted Solution

by:Craig Wagner
Craig Wagner earned 167 total points
ID: 39882880
The following will tell you if Excel is running. Replace "excel" with the name of the process you're looking for.

Dim isProcessRunning = Process.GetProcesses().Any(Function(p) p.ProcessName.Equals("excel", StringComparison.OrdinalIgnoreCase))

Open in new window

0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 167 total points
ID: 39882882
Use Process.GetProcessesByName():
        Dim executable As String = "c:\some folder\path\file.exe"
        If Process.GetProcessesByName(System.IO.Path.GetFileNameWithoutExtension(executable)).Length = 0 Then
            Process.Start(executable)
        End If

Open in new window

*Note that you pass just the EXE name without the path and without the .exe part.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39882905
>>Important not to have two instances of the same App at the same time.

The only way I know of to be certain only instance is running is to modify the app so that once it is started it checks to see if itself is already running and then closes the instance that has just started.
pseudo code:
main()
{
  If(!AlreadyRunning())  //if it is already running then the current startup code is not run
  {
    //current startup code
  }
}
0
 
LVL 34

Assisted Solution

by:sarabande
sarabande earned 166 total points
ID: 39883102
to add to before comment:

the AlreadyRunning function could be performed by using a named mutex. a mutex is a system resource which would be available as long as there is a program which has a handle of it. so a program that claims a handle of the mutex by name can find out whether it created the mutex or whether there is another program that created the mutex before. if that is the case the new program would exit.

the vb code could be like (not tested):

Imports System.Threading

Dim objMutex As Mutex

objMutex = New Mutex(False, "Use_Here_a_Unique_Name_No_Spaces")
If objMutex.WaitOne(0, False) = False Then
       objMutex.Close()
       objMutex = Nothing
       MessageBox.Show("Instance already running")
       End
 End If
 'if you get to this point it is the first (and only) instance
      'continue with app

Open in new window


Sara
0
 

Author Closing Comment

by:rguillermo
ID: 39884264
Thank you !!!
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39884802
Odd choice of answers unless it isn't so important not to have two instances running after all.
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

688 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