How to close a WPF application window if a new instance of the application open from browser using Custom URI Scheme

Mahesh C M 151 Reputation points
2021-06-08T12:06:46.823+00:00

Hi,

I have a windows desktop application developed in WPF. In the app itself I have register the custom URI scheme. When my application is already running and i tried to send some arguments from browser using custom URI scheme and the app will open in another instance. Is there any option to close the default instance keep the new one send from browser or keep the default instance and close the new one and also sending those parameter value to the default app which is running.

Please anyone help me on this

Thanks in advance !

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,761 questions
{count} votes

Accepted answer
  1. Viorel 116.6K Reputation points
    2021-06-09T13:34:02.567+00:00

    If your application is visible (has the main window), then you can activate the running instance by handling the Startup event (in App.xaml and App.xaml.cs files in case of C#):

    private void App_Startup( object sender, StartupEventArgs e )
    {
       var current_process = Process.GetCurrentProcess( );
       var other_process = Process.GetProcessesByName( current_process.ProcessName ).FirstOrDefault( p => p.Id != current_process.Id );
    
       if( other_process != null && other_process.MainWindowHandle != IntPtr.Zero )
       {
          if( IsIconic( other_process.MainWindowHandle ) )
          {
             ShowWindow( other_process.MainWindowHandle, SW_RESTORE );
          }
          SetForegroundWindow( other_process.MainWindowHandle );
          Shutdown( );
       }
    }
    
    [DllImport( "user32" )]
    static extern bool IsIconic( IntPtr hWnd );
    
    [DllImport( "user32" )]
    static extern bool ShowWindow( IntPtr hWnd, int cmdShow );
    const int SW_RESTORE = 9;
    
    [DllImport( "user32" )]
    static extern bool SetForegroundWindow( IntPtr hWnd );
    

    This code will activate the existing window and will close the new one.

    If you know details about Windows programming, you can use process.MainWindowHandle, SendMessage, PostMessage, etc. to transfer some custom messages (e.g. WM_APP+1 or WM_COPYDATA) with information about parameters.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. DaisyTian-1203 11,621 Reputation points
    2021-06-09T09:30:12.007+00:00

    You could add a Load event for the new instance to close the old instance . For example, open Winodw2 and close Window1, you could add Loaded += Window2_Loaded; for Window2 constructor to close Window1, then implement it as below:

    private void Window2_Loaded(object sender, RoutedEventArgs e)  
            {  
                foreach (Window window in Application.Current.Windows)  
                {  
                    if (window.GetType() == typeof(Window1))  
                    {  
                        window.Close();  
                    }  
                }  
            }  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.