• Skip to main content
  • Skip to primary sidebar

Technical Notes Of
Ehi Kioya

Technical Notes Of Ehi Kioya

  • Forums
  • About
  • Contact
MENUMENU
  • Blog Home
  • AWS, Azure, Cloud
  • Backend (Server-Side)
  • Frontend (Client-Side)
  • SharePoint
  • Tools & Resources
    • CM/IN Ruler
    • URL Decoder
    • Text Hasher
    • Word Count
    • IP Lookup
  • Linux & Servers
  • Zero Code Tech
  • WordPress
  • Musings
  • More
    Categories
    • Cloud
    • Server-Side
    • Front-End
    • SharePoint
    • Tools
    • Linux
    • Zero Code
    • WordPress
    • Musings
Home » Backend (Server-Side) » Prevent Multiple Instances of Same Application in C#

Prevent Multiple Instances of Same Application in C#

By Ehi Kioya 3 Comments

By default, .NET windows forms applications generally allow multiple instances of the same application to be started on the same machine. However, if two or more threads attempt to access a shared resource such as shared memory at the same time, concurrency issues may occur. And in a production environment, this might result in data inconsistency or inaccuracy.

This problem can be avoided using mutual exclusion (Mutex) to ensure that simultaneous instances of the same program cannot be run.

The code below demonstrates how this may be done.

static void Main()
{
   bool createdNew = true;
   using (Mutex mutex = new Mutex(true, "DemoApp", out createdNew))
   {
      if (createdNew)
      {
         Application.EnableVisualStyles();
         Application.SetCompatibleTextRenderingDefault(false);
         Application.Run(new Form1());
      }
      else
      {
         MessageBox.Show("The Application Is Already Running", "DemoApp", MessageBoxButtons.OK, MessageBoxIcon.Error);
      }
   }
}

Multiple Instance Image

The message shown in the picture above is displayed to the user if he attempts to start an instance of the program when another instance of the same program is already running.

Found this article valuable? Want to show your appreciation? Here are some options:

  1. Spread the word! Use these buttons to share this link on your favorite social media sites.
  2. Help me share this on . . .

    • Facebook
    • Twitter
    • LinkedIn
    • Reddit
    • Tumblr
    • Pinterest
    • Pocket
    • Telegram
    • WhatsApp
    • Skype
  3. Sign up to join my audience and receive email notifications when I publish new content.
  4. Contribute by adding a comment using the comments section below.
  5. Follow me on Twitter, LinkedIn, and Facebook.

Related

Filed Under: Backend (Server-Side), C# Tagged With: CSharp, Programming

About Ehi Kioya

I am a Toronto-based Software Engineer. I run this website as part hobby and part business.

To share your thoughts or get help with any of my posts, please drop a comment at the appropriate link.

You can contact me using the form on this page. I'm also on Twitter, LinkedIn, and Facebook.

Reader Interactions

Comments

  1. sdfs says

    March 5, 2015 at 4:50 am

    very good its working

    Reply
  2. Chanchal says

    October 10, 2016 at 1:03 pm

    Will it work when application is installed on network and multiple instance has been instantiated.
    Your example is working when application in installed on local but I tried it on sharing path and there it is failing.
    Please suggest me if you have any solution.
    Thanks

    Reply
    • Ehi Kioya says

      October 10, 2016 at 1:22 pm

      Hi Chanchal,

      The mutex implementation described here will not work as you want across network computers.
      To do the sort of mutex you want, you’ll need some form of communication between your clients and a central point. A database would be a good “central point”. Does your application already use a database?

      Thanks.

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

25,641
Followers
Follow
30,000
Connections
Connect
14,582
Page Fans
Like

POPULAR   FORUM   TOPICS

  • How to find the title of a song without knowing the lyrics
  • The Art of Exploratory Data Analysis (Part 1)
  • Welcome Message
  • How To Change Or Remove The WordPress Login Error Message
  • Getting Started with SQL: A Beginners Guide to Databases
  • Replacing The Default SQLite Database With PostgreSQL In Django
  • How to Implement Local SEO On Your Business Website And Drive Traffic
  • How to see other devices logged into your Facebook account
  • How To Check If Your Computer Can Run A Game Or Application
  • Python for Data Science
  • Recently   Popular   Posts   &   Pages
  • Actual Size Online Ruler Actual Size Online Ruler
    I created this page to measure your screen resolution and produce an online ruler of actual size. It's powered with JavaScript and HTML5.
  • Allowing Multiple RDP Sessions In Windows 10 Using The RDP Wrapper Library Allowing Multiple RDP Sessions In Windows 10 Using The RDP Wrapper Library
    This article explains how to bypass the single user remote desktop connection restriction on Windows 10 by using the RDP wrapper library.
  • WordPress Password Hash Generator WordPress Password Hash Generator
    With this WordPress Password Hash Generator, you can convert a password to its hash, and then set a new password directly in the database.
  • Forums
  • About
  • Contact

© 2021   ·   Ehi Kioya   ·   All Rights Reserved
Privacy Policy