前回の記事:
WPF アプリで、二重起動を禁止する
今回は、フォームアプリを調べてみました。
フォームアプリは、メインエントリポイントが Program.cs にあるのでシンプルです。
using System;
using System.Reflection;
using System.Threading;
using System.Windows.Forms;
static class Program
{
[STAThread]
static void Main()
{
Mutex mutex = null;
var isSingleInstance = Properties.Settings.Default.IsSingleInstance;
if (isSingleInstance)
{
var name = Assembly.GetEntryAssembly().GetName().Name;
mutex = new Mutex(false, name);
if (!mutex.WaitOne(TimeSpan.Zero, false))
{
mutex.Close();
return;
}
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
if (mutex != null)
{
mutex.ReleaseMutex();
mutex.Close();
}
}
}
WPF アプリより簡単ですね。
頑張りすぎず脱力系でいこうと思います。
以上。
0 件のコメント:
コメントを投稿