アプリケーションの多重起動を禁止しちゃう。
いつになく無性にアプリケーションの多重起動を禁止したくなったときは Mutex クラス(System.Threading 名前空間)を使用するのが一般的なようなのでメモ。
[Visual Basic]Imports System
Imports System.Threading
Imports System.Windows.FormsNamespace Torikobito
Public Class Program
<STAThread()> _
Public Shared Sub Main()Dim aMutex As Mutex = Nothing
Dim createdNew As Boolean = FalseTry
' 一意の識別名をつけて Mutex クラスのインスタンスを作成。
' 初期所有権が付与された場合、createdNew は True が格納される。
aMutex = New Mutex(False, "Torikobito", createdNew)
If Not createdNew ThenExit Sub
End If
Using mainForm As New Form
Application.Run(mainForm)
End Using
Finally
If aMutex IsNot Nothing Then
aMutex.Close()
End If
End Try
End Sub
End Class
End Namespace
[C#]using System;
using System.Threading;
using System.Windows.Forms;
namespace Torikobito
{
public static class Program
{
[STAThread()]
public static void Main()
{
Mutex aMutex = null;
bool createdNew = false;try
{
// 一意の識別名をつけて Mutex クラスのインスタンスを作成。
// 初期所有権が付与された場合、createdNew は True が格納される。
aMutex = new Mutex(false, "Torikobito", createdNew);if (!createdNew)
{
return;
}using (mainForm = new Form())
{
Application.Run(mainForm);
}}
finally
{
if(aMutex != null )
{
aMutex.Close();
}
}
}
}
}