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
Solved

control if a process is being executed visual basic 2012

Posted on 2014-02-24
8
409 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
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 85

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 33

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying 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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

860 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