?
Solved

control if a process is being executed visual basic 2012

Posted on 2014-02-24
8
Medium Priority
?
425 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 45

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 668 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
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 86

Accepted Solution

by:
Mike Tomlinson earned 668 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 45

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 35

Assisted Solution

by:sarabande
sarabande earned 664 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 45

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

750 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