Skip to content

steven777400/WPFSingleInstanceApplication

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 

Repository files navigation

Enforcing Single Application Instances in WPF with Named Pipes

See blog post.

Usage:

    public partial class App : Application
    {
        
        protected override void OnStartup(StartupEventArgs e)
        {
            // hook on error before app really starts
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
            base.OnStartup(e);

            // Set a unique application ID
            Guid id = Guid.Parse("DC2A927C-AC89-4512-BB29-7AB0A18DE105");

            // Instantiate an SIA
            SingleInstanceApplication sia = new SingleInstanceApplication(id);            
            // Handle the ApplicationStarts event
            // When this event fires, initialize the application
            sia.ApplicationStarts += (sender, earg) =>
            {
                var mw = new MainWindow();
                mw.Show();
                // If another instance attempts to start, bring our window to the front
                sia.AnotherInstanceAttemptsToStart += SingleInstanceApplication.BringToFrontWhenCalled(mw);
            };
            // Optionally handle AnotherInstanceAttemptsToStart, for example, to log other attempts
            sia.AnotherInstanceAttemptsToStart += (sender, earg) =>
            {
                Logger.LogInfo("### Captured another instance trying to start");

            };
            // Run the application and single instance protection
            sia.RunSingleInstance();            

        }

About

Enforcing Single Application Instances in WPF with Named Pipes

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages