Run Only One Copy Of Application

This is a class to make an application where only one instance of app can be run at a time in C#. The method must be called in  Program.cs by replacing: 

minus.gif  Collapse
Application.Run(new Form1());
with 

minus.gif  Collapse
Chico.SingleApp.Initialize(true, new Form1());
a shorter version(where this initial code originated from) was posted by: Ron Whittle and can be found at http://www.daniweb.com/software-development/csharp/code/352511[ ^]

minus.gif  Collapse
using System;
using System.Threading;
using System.Windows.Forms;
namespace Chico
{
public class SingleApp
{
private static Boolean IsSingleAppMode;
private static Mutex singleApp;
private const Int32 waitTime = 3000;
public static Boolean Initialize(Boolean value, Form form)
{
try
{
if (value)
{
using (singleApp = InitializeSingleAppMode(IsSingleAppMode))
{
if (StartAsSingleAppMode(IsSingleAppMode))
{
StartApplication(form);
}
}
}
else
{
StartApplication(form);
}
}
catch { }
return value;
}
private static Boolean StartAsSingleAppMode(Boolean result)
{
return singleApp.WaitOne(waitTime, result);
}
private static void StartApplication(Form mainForm)
{
Application.Run(mainForm);
}
private static Mutex InitializeSingleAppMode(Boolean result)
{
return new Mutex(result, "anydomain.com myprogramname", out IsSingleAppMode);
}
}
}
If you like the code or plan to use the code, please vote for me and stop by  http://www.daniweb.com/software-development/csharp/code/352511[ ^] and take a look at the original code by Ron Whittle. Thanks and hope you like the code.
Posted 18 Mar '11
Edited 19 Mar '11
Revision 3

red.gifred.gifred.gifred.gifred.gif
 5.00 / 5, 2 votes
Sign Up to vote for this article
 

Alternate 1

 

Yet another way to this is as follows:

minus.gif  Collapse
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);
}
}
}
Take a look  there[ ^] to read the complete article.
   link    Bookmark
   Revision 3
 
charles henington  -  19 Mar '11
True that is another way to do the same thing. The reason that I built it the way that i have is so the code will be in a class file so that it can be included in a framework
 
AspDotNetDev  -  20 Mar '11
I think I prefer this technique over the original because the original will call the Form1 constructor first, while this technique will avoid that if the program is already running.
 
Nenad L.  -  22 Mar '11
Hmm... just a quick thought: regarding Windows 7 (and perhaps vista, too, dunno), the Mutexes should be global.
 
ashishkumar008  -  5 days ago
Reason for my vote of 5 good-good, very good.
red.gifred.gifred.gifred.gifred.gif
 5.00 / 5, 1 vote
Sign Up to vote for this article
 

Alternate 2

 

Maybe this is helpful, it tries to switch to the first running instance:

minus.gif  Collapse
namespace UltraSimple.Win
{
static class Program
{
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);

[STAThread]
static void Main()
{
Process oProcess = Process.GetCurrentProcess();
Process[] aoProcesses = Process.GetProcessesByName(oProcess.ProcessName);
if (aoProcesses.Length > 1)
{
MessageBox.Show(
"The application \""
+ oProcess.ProcessName
+ "\" is already running.", "Ultrasimple");
Program.SetForegroundWindow(aoProcesses[aoProcesses[0].Id ==
oProcess.Id ? 1 : 0].MainWindowHandle);
return;
}
...
}
}
}
   link    Bookmark
   Revision 2
red.gifred.gifred.gifred.gifwhite.gif
 4.00 / 5, 1 vote
Sign Up to vote for this article
 

Alternate 3

 

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).
minus.gif  Collapse
string processName = Process.GetCurrentProcess().ProcessName;
Process[] instances = Process.GetProcessesByName(processName);

if (instances.Length > 1)
{
//Try and send a message with args[]
StringBuilder text = new StringBuilder();
foreach (string str in args)
{
if (File.Exists(str)) //If it is a file, I want full path
{
text.Append(new FileInfo(str).FullName);
}
else
{
text.Append(str);
}
text.Append(' ');
}
if (text.Length > 0)
{
text.Remove(text.Length - 1, 1);
}
Remote.Send(text.ToString());
// MessageBox.Show("This application is already running");
return;
}
   link    Bookmark
   Revision 2
 
Nenad L.  -  22 Mar '11
Be aware that this Code won't work in Terminal Server Sessions.

转载于:https://www.cnblogs.com/errorx/archive/2011/03/29/1999116.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值