Mutexクラスの説明
オーバーロード。 派生クラスでオーバーライドされると、現在のWaitHandleがシグナルを受信するまで現在のスレッドをブロックします。 Overloads Public Overridable Function WaitOne( _ ByVal millisecondsTimeout As Integer, _ ByVal exitContext As Boolean _ ) As Boolean
Imports System.Threading Imports system.windows.forms Public Shared Sub Main() ' Mutexのインスタンスを生成する Dim m As New Mutex(False, Application.ProductName) ' 既にMutexが取得されていないか確認する if not m.waitone(0, false) then ' 既に取得されている場合 console.writeline( "Application already executed.") ' mutexを解放 m.close() ' ここまでで処理を終了する return end if ' 複数起動されていない場合 console.writeline("run application") ' mutexを解放 m.ReleaseMutex() End Sub
using System.Threading; using System.WIndows.Forms; public static void Main() { Mutex m = new Mutex(false, Application.ProductName); if ( !m.waitone(0,false) ) { console.writeline( "Application already executed.") m.close() return; } Console.WriteLine("run application") m.ReleaseMutex() }
Mutexの排他アクセス機能を利用することで、アプリケーションの多重起動を阻止できる。 Mutexコンストラクタの二つ目の引き数(ここではApplication.ProductName)は適当な文字列に変えることが出来る。