What you want to see on DotNetFunda.com ?
DotNetFunda.Com Logo
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 37596 |  Welcome, Guest!   Register  Login
 Home > Code Snippets > Windows Forms > Run Only One Copy Of Application ...
RaviRanjanKr

Run Only One Copy Of Application

 Code Snippet posted by: RaviRanjanKr | Posted on: 7/11/2011 | Category: Windows Forms Codes | Views: 1362 | Status: [Member] | Points: 40 | Alert Moderator   


This Code Snippet allow you to run only one Instance of application at a time.
Many time we have requirement that we need to run only one instance of application at a time. If you have same requirement then in that case this Code Snippet might be useful for you.
Here you will learn how to handle only one instance of application by using Mutex

// This is default Program.Cs where Entry point (Main method) lies
using System;

using System.Collections.Generic;
using System.Windows.Forms;
using System.Threading;
namespace OnlyOneInstance
{
static class Program
{
[STAThread]
static void Main()
{
bool instantiated;
/* If instantiated is true, this is the first instance of the application; else, another instance is running. */
Mutex mutex = new Mutex(true, "UniqueID", out instantiated);
if (!instantiated)
{
MessageBox.Show("Already Running");
return;
}
Application.Run(new Form1());
GC.KeepAlive(mutex);
}
}
}


You can download Source code from given link to getting more details
http://dl.dropbox.com/u/27553051/OnlyOneInstance.zip

After downloading Go to bin directory and then debug OnlyOneInstance.exe file twice, you will see a message box which stated Program Already Running .

Thanks
Ravi Ranjan Kumar
http://raviranjankr.wordpress.com
Found interesting? Add this to:


>> Write Response - Respond to this post and get points

More codes snippets

About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/24/2013 1:46:52 PM