Solved

control if a process is being executed visual basic 2012

Posted on 2014-02-24
8
408 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# parent child form 5 28
Reading the Web.Config using IIS 7.5? 4 35
C# Gridview 1 29
How do i run a c++ file? 15 25
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…

776 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