Skip to content

Add SingleInstanceManager: mutex-based single instance with named-pipe window activation - #122

Merged
Soar360 merged 3 commits into
mainfrom
copilot/add-single-instance-manager
Jul 14, 2026
Merged

Add SingleInstanceManager: mutex-based single instance with named-pipe window activation#122
Soar360 merged 3 commits into
mainfrom
copilot/add-single-instance-manager

Conversation

Copilot AI commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

Adds SingleInstanceManager to LuYao.Threading — a static helper that enforces single-process execution via a global named mutex and uses a named pipe to signal the existing instance to activate its window.

Implementation

  • src/LuYao.Common/Threading/SingleInstanceManager.cs

    • Global mutex name: Global\LuYao.SingleInstance[{uid}] where uid is the MD5 hash of Environment.ProcessPath (NET6+) / AppContext.BaseDirectory — unique per application binary
    • Pipe name: LuYaoSI{uid} (no backslash, safe across all pipe implementations)
    • First instance starts a long-running background server thread that listens for ActivateWindow commands and fires ActivateWindowRequested
    • Subsequent instances connect to the pipe, send ActivateWindow, then Environment.Exit(0)
    • ReleaseLock() allows explicit teardown (e.g. for restart scenarios)
    • Cross-framework: #if NETFRAMEWORK guards for WaitForConnectionAsync and #if NET45 / #if NET6_0_OR_GREATER guards for AppContext/Environment.ProcessPath
  • tests/LuYao.Common.UnitTests/Threading/SingleInstanceManagerTests.cs — covers ReleaseLock idempotency, event subscribe/unsubscribe, and named pipe round-trip

  • README.zh-CN.md / README.en.md — added section 13 documenting SingleInstanceManager

Usage

// Program startup
SingleInstanceManager.EnsureSingleInstance();

// Subscribe before or after — fired when a second instance starts
SingleInstanceManager.ActivateWindowRequested += (_, _) =>
{
    mainWindow.Activate();
    mainWindow.WindowState = WindowState.Normal;
};

Copilot AI linked an issue Jul 14, 2026 that may be closed by this pull request
Copilot AI changed the title [WIP] Add single instance manager with mutex Add SingleInstanceManager: mutex-based single instance with named-pipe window activation Jul 14, 2026
Copilot AI requested a review from Soar360 July 14, 2026 09:07

@Soar360 Soar360 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread src/LuYao.Common/Threading/SingleInstanceManager.cs Outdated
Copilot AI requested a review from Soar360 July 14, 2026 09:12
@Soar360
Soar360 marked this pull request as ready for review July 14, 2026 09:22
Copilot AI review requested due to automatic review settings July 14, 2026 09:22

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@Soar360
Soar360 merged commit da6854f into main Jul 14, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

添加单实例管理器

3 participants