Click here to Skip to main content
15,892,059 members
Articles / Desktop Programming / Windows Forms

Run Only One Copy Of Application

Rate me:
Please Sign up or sign in to vote.
4.92/5 (22 votes)
6 Nov 2011CPOL 28K   16  
Yet another way to this is as follows: using System;using System.Collections.Generic;using System.Windows.Forms;using System.Threading;namespace OnlyOneInstance{ static class Program { [STAThread] static void Main() { bool...

Alternatives

Members may post updates or alternatives to this current article in order to show different approaches or add new features.

Please Sign up or sign in to vote.
12 Apr 2011KevinPorter
How about using a Mutex? This should also work for Terminal Services sessions. Use in Program.csstatic Mutex mut;try{ bool isOwned = false; mut = new Mutex(true, Application.ProductName + " MUTEX: {53A4988C-F91F-4054-9076-220AC5EC03F3}", out isOwned); if (!isOwned)...
Please Sign up or sign in to vote.
25 Mar 2011FDW
Maybe this is helpful, it tries to switch to the first running instance:namespace UltraSimple.Win{ static class Program { [System.Runtime.InteropServices.DllImport("user32.dll")] public static extern bool SetForegroundWindow(IntPtr hWnd); [STAThread] ...
Please Sign up or sign in to vote.
19 Mar 2011charles henington 5 alternatives  
Run Only One Copy Of Application
Please Sign up or sign in to vote.
30 Mar 2011tlhIn`toq
Just because we like C# doesn't mean we can't borrow from other .NET languages. I've always had good luck and less complication using VB's single instance methodology in my C# applications. This code would go in your program.cs file:/// /// We inherit from...
Please Sign up or sign in to vote.
29 Mar 2011Neil A. Harding
I actually use code like this, which allows me to detect a existing instance and to call the original instance with the command line (I expand any filenames present so that it can access the filenames even if the original path is different).string processName =...

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions