应用程序单例

应用程序单例可以通过下面的几种方法来实现:

1.使用Mutex类

2.使用Semphore类

3.使用EventWaitHandle类

4.继承Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase

 

其中使用Semphore能控制应用程序能够启动的实例的个数,

继承Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase类能够很方便的在下一个实例启动的时候执行相关的代码.

下面分别给出相应的实现代码.例子使用的都是WinForm应用程序.Form1是一个新建的空的窗体.

Demo下载地址:http://files.cnblogs.com/loyldg/SingltonAppDemo.rar

1.使用Mutex

 

Codeusing System;
using System.Threading;
using System.Windows.Forms;

namespace SingletonAppUsingMutex
{
    static class Program
    {
        /// <summary>
        /// 如果名称以前缀"Global\"开头,则Mutex在所有中端服务器会话中均为可见.如果名称以前缀"Local\"开头,则mutex仅在创建它的终端服务器会话中可见(MSDN)
        /// </summary>
        private static string name = @"Global\singletonAppDemo";

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            
            bool createdNew;            
            Mutex mutex = new Mutex(true,name , out createdNew);

            if (createdNew)
            {
                Application.Run(new Form1());
            }
            else
            {
                MessageBox.Show("Application already running","warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }      
    }
}



2.使用Semphore

Codeusing System;
using System.Threading;
using System.Windows.Forms;

namespace SingletonAppUsingSemaphore
{
    
    static class Program
    {
        private static Semaphore _semaphore;
        private static readonly int maxCount = 3;
        private static readonly int initCount = 3;
        private static string name = "namedSemaphore";

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            bool isNotReachMax;
            _semaphore = new Semaphore(initCount, maxCount, name);
            isNotReachMax = _semaphore.WaitOne(0, true);

            if (isNotReachMax)
            {
                Application.Run(new Form1());
            }
            else
            {
                MessageBox.Show(string.Format("Application already running more than {0} instances", maxCount), "warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
    }     
}


3.使用EventWaitHandle

Codeusing System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Threading;


namespace SingletonAppUsingEventWaitHandle
{
    static class Program
    {
        private static string name = "SingletonAppUsingEventWaitHandle";

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            bool createdNew;
            EventWaitHandle ewh = new EventWaitHandle(true, EventResetMode.ManualReset, name, out createdNew);

            if (createdNew)
            {
                Application.Run(new Form1());
            }
            else
            {
                MessageBox.Show("Application already running", "warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);                
            }
        }
    }
}


 

4.继承Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase

App.cs

Codeusing System.Windows.Forms;

namespace SingletonUsingWindowsFormsApplicationBase
{
    public class App:Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase
    {
        public App()
        {
            this.IsSingleInstance = true;
        }

        protected override bool OnStartup(Microsoft.VisualBasic.ApplicationServices.StartupEventArgs eventArgs)
        {
            Application.Run(new Form1());

            return false;
        }

        protected override void OnStartupNextInstance(Microsoft.VisualBasic.ApplicationServices.StartupNextInstanceEventArgs eventArgs)
        {
            var form = Application.OpenForms[0];
            form.WindowState = FormWindowState.Normal;
            form.Show();
            form.Activate();

        }
    }
}


Program.cs

Codeusing System;
using System.Windows.Forms;

namespace SingletonUsingWindowsFormsApplicationBase
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            App app = new App();
            app.Run(args);
        }
    }
}


转载于:https://www.cnblogs.com/loyldg/archive/2011/04/14/2015533.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值