C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » .NET Framework »

How to restrict a program to a single instance


Posted Date: 01 Oct 2004    Resource Type: Articles    Category: .NET Framework
Author: Vimal Kanth Member Level: Silver    
Rating: 1 out of 5Points: 5



Include System.Diagnostics namespace.

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

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.

Here is the code :


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: Venkatarajan Arthanari    01 Oct 2004Member Level: Silver   Points : 0
Good article.

Venkatarajn

Lot to Learn,
Too Little Time.


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

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.
Next Resource: Model Driven Architecture (MDA)
Previous Resource: Exploit .NET Support in Yukon
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
Related Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use