C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




How to restrict a program to a single instance


Posted Date: 23 Feb 2005    Resource Type: Code Snippets    Category: Process

Posted By: saji       Member Level: Gold
Rating:     Points: 7



The following are the steps involved in restricting a program to a single instance :


  1. Create an instance of the Process class to store the current running process. GetCurrentProcess() will create a new process component and assign it to the current process we are running.

  2. Create another instance for Process class.Check whether already the same process in running using GetProcessesByName method.

    GetProcessesByName method creates an array of process and associates them with all the process component that share the specified process name. Check if the length of the array object is greater than one.If it is then current process is already instantiated. Otherwise allow the process to run.


The code sample below follows the above given algorithm judiciously. In this example, we have implemented the algorithm in the Main() function. This can also be treated as a seperate method if necessary. For example, when a user chooses to run a program, say a music player, which is already running, we might not need another instance of the player. Thus, every time a process is instantiated we need to check if it's already running.



static void Main()
{
Process ThisProcess = Process.GetCurrentProcess();
Process [] AllProcesses = Process.GetProcessesByName(ThisProcess.ProcessName);
if (AllProcesses.Length > 1)
{
MessageBox.Show(ThisProcess.ProcessName + " is already running",
ThisProcess.ProcessName,
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}else {
Application.Run(new MainForm());
}
}






Responses

Author: Initiotech    31 Jul 2008Member Level: Gold   Points : 2
Good Work Here is an VB.net Version of the Same Code

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Process.GetProcessesByName(Process.GetCurrentProcess.ProcessName).Length() > 1 Then
MsgBox("This is a Single instance Applicaton " & vbNewLine & "* Another Instance is Already Running", MsgBoxStyle.OKOnly, "Single Instance Application")
End
End If
End Sub


Thank You

Regards Hefin Dsouza


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Restricting instances of a program  .  How to restrict a program to a single instance  .  Avoiding multiple instances of a program  .  Avoiding duplicate processes  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Previous Resource: Many ways to get your application path
Return to Discussion Resource Index
Post New Resource
Category: Process


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

web conferencing services

Contact Us    Privacy Policy    Terms Of Use