SQL Server administration and T-SQL development, Web Programming with ASP.NET, HTML5 and Javascript, Windows Phone 8 app development, SAP Smartforms and ABAP Programming, Windows 7, Visual Studio and MS Office software Kodyaz Development Resources
Development resources, articles, tutorials, samples, codes and tools for .Net, SQL Server, Windows, Windows Phone, SAP and ABAP, like SAP UI5, Screen Personas, etc.

.NET Framework, ASP.NET

ASP.NET, Windows Forms, Controls, .NET Framework and Visual Studio Articles

ASP.NET Forums

.NET Development Blog

How to check that a unique instance of a process is running

Microsoft .NET programmers may require to check that a unique instance of a process is running on the computer within their .NET code.

One way of doing this task is enumerating all processes running on the computer and then check each process whether it is an instance of the current application.

For this task, .NET developers can use the Process class, a member of the System.Diagnostics namespace.

Process class provides access to local and remote processes and enables the user to start and stop local system processes.

Process class has a public shared function named GetCurrentProcess() which returns the current active process into a new Process object.

So by the following line of code :

Dim p As Process = Process.GetCurrentProcess

We can get the current running application as a process object which is an instance of System.Diagnostics.Process class.

Process class has some properties to note, one is ProcessName which is the name of the process. Another property is the Id of a process which is the unique identifier for the related process. That is the PID column value diplayed in Windows Task Manager Processes screen.

MachineName property gives the computer name on which the process is running. "." is the computer name if the process is running on the local computer.

StartTime property is the time that the process has started. And by using the Kill method you can stop the execution of the process immediately. If you use the public shared GetProcessesByName method, you will get an array of the processes running that have the same name defined as the input parameter of the method. You will see a sample for GetProcessesByName method in the following line of code.

Dim processList As Process() = Process.GetProcessesByName(processName)

If you want to get all processes list running on a computer, then you can use public shared GetProcesses method. GetProcesses method will return an array containing a Process object for each of the processes running on the computer. If you know the process id of the process that you want to get more detailed information about the related process, then you can use GetProcessById method with the ProcessId as the input parameter.

Till here we have summarized main properties and methods of the Process class. Now we can focus on how we can use the process properties to know whether only one instance of the application or the process is running on a computer.

The simplest way of doing this is getting the current process name and then getting the list of all processes that has the same name and running on the computer. If the list has more than one item, then we can say that there is more than one instance of the associated process running on the computer.

Check the below code script. This method for solving this problem ignores that different applications may have the same name.

Public Shared Function IsUniqueProcessRunning() As Boolean
  Dim p As Process = Process.GetCurrentProcess
  Dim processName As String = p.ProcessName
  Dim processList As Process() = Process.GetProcessesByName(processName)
  If processList.Length = 1 Then
    IsUniqueProcessRunning = True
    IsUniqueProcessRunning = False
  End If
End Function

Copyright © 2004 - 2017 Eralper YILMAZ. All rights reserved.
Community Server by Telligent Systems