0
私は、メインクラスの.net 4.5とmutexを使用して、単一インスタンスのWinFormアプリケーションを開発しました。ミューテックスがすでに使用されているため、アプリケーションを起動できないという報告があります。シングルインスタンスアプリケーションが起動していない
static string guid = ((GuidAttribute)Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(GuidAttribute), true)[0]).Value + "-OrientalWave";
public static string GUID { get { return guid; } }
[STAThread]
static void Main(string[] args)
{
AppDomain currentDomain = AppDomain.CurrentDomain;
currentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledExceptionHandler);
// Single Instance Check
bool createdNew;
using (var mutex = new Mutex(true, guid, out createdNew))
{
if (createdNew)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
if (args.Length > 0)
{
string filename = args[0];
Application.Run(new frmMain(filename));
}
else
Application.Run(new frmMain());
}
else
{
if (args.Length > 0)
{
// If i want to open a file
string filename = args[0];
NamedPipesClient pipeClient = new NamedPipesClient();
pipeClient.Start();
pipeClient.SendMessage(filename);
pipeClient.Stop();
}
else
MessageBox.Show("Only single instance allowed", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
static void UnhandledExceptionHandler(object sender, UnhandledExceptionEventArgs args)
{
try
{
var exception = (Exception)args.ExceptionObject;
MessageBox.Show(exception.Message, null, MessageBoxButtons.OK, MessageBoxIcon.Error);
Logger.Write(Logger.LogType.Error, exception.Source, "Message: " + exception.Message + " - Stack: " + exception.StackTrace);
}
catch { }
Environment.Exit(0);
}
Webを検索して、単一のインスタンスアプリケーションを作成することはできましたが、成功しませんでした。
アプリの機能、それほどひどく驚くべきことではないと判断見つけることができます。このホイールの再発明を避けてください。すでにフレームワークによってサポートされています。毎日数百万回のテストを受けている何十万人ものプログラマに悩まされているコードを常に優先します。 https://stackoverflow.com/a/29260770/17034 –
私の意見では、このコードは期待どおりに動作するはずです... – Legends