■■■

2016年4月9日土曜日

Mutexで確実に二重起動を禁止する方法

Mutexで確実に二重起動を禁止する方法
VB.NET
    ''' <summary>
    ''' アプリケーションのメイン エントリ ポイントです。
    ''' </summary>
    <STAThread> _
    Private Shared Sub Main()
        ' 初期化
        Dim mtxApp As New System.Threading.Mutex(False, Application.ProductName)

        ' ミューテックスシグナルの判定
        If mtxApp.WaitOne(0, False) Then
            ' アプリを起動する
            Application.Run(New Form1())
        End If

        ' ガーベージコレクションに設定
        GC.KeepAlive(mtxApp)

        ' オブジェクト破棄
        mtxApp.Close()

    End Sub


C#
        /// <summary>
        /// アプリケーションのメイン エントリ ポイントです。
        /// </summary>
        [STAThread]
        static void Main()
        {
            // 初期化
            System.Threading.Mutex mtxApp = new System.Threading.Mutex(false, Application.ProductName);

            // ミューテックスシグナルの判定
            if (mtxApp.WaitOne(0, false))
            {
                // アプリを起動する
                Application.Run(new Form1());
            }

            // ガーベージコレクションに設定
            GC.KeepAlive(mtxApp);

            // オブジェクト破棄
            mtxApp.Close();

        }

■■■