1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

C#でClickOnceで配布したアプリの二重起動を防止したい

Posted at

1. はじめに

  • C#のクライアントアプリの二重起動を防止したい
  • ClickOnceの場合にMutexを使用した方法でうまく行かなかったため、他の方法で実施する方法を検討したい

2. 開発環境

  • C#
  • .NET 6
  • Visual Studio 2022
  • Windows 11

3. 二重起動防止方法

3.1. Mutexを使用した場合

  • 原因が不明だがClickOnce経由だと想定通り動作しなかった
Program.cs(抜粋)
// Mutext作成
    Mutex app_mutex = new Mutex(false, Appication.ProductName);

// Mutextの所有権を要求する(二重起動を判定する)
if (app_mutex.WaitOne(0, false) == false) {
    MessageBox.Show("Software is already running.");
    return;
}

3.2. プロセス名を使用した場合

  • ClickOnceで想定通り動作したが、レスポンスの問題やセキュリティ上あまりよくないのでMutexを使用する方法を検討したい
Program.cs(抜粋)
// Processからプロセス名を取得する
Process p = Process.GetCurrentProcess();
Process [] processSearch = Process.GetProcessesByName(p.ProcessName);

// 二重起動を判定する
if (processSearch.Length > 1)
{
    MessageBox.Show("Software is already running.");
    return;
}

4. 参考文献

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?