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

1.使用Mutex类

2.使用Semphore类

3.使用EventWaitHandle类

4.继承Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase

 

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

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

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

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

1.使用Mutex

 

using 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

using 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

using 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

using 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

using 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);
        }
    }
}


相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-11
  • 2021-06-04
  • 2022-12-23
  • 2021-11-07
猜你喜欢
  • 2022-12-23
  • 2022-03-01
  • 2021-11-16
  • 2021-11-26
  • 2021-07-28
  • 2022-12-23
  • 2021-10-08
相关资源
相似解决方案