question

MaheshCM-7927 avatar image
0 Votes"
MaheshCM-7927 asked Viorel-1 edited

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

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-wpf
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@MaheshCM-7927
I'm sorry I'm a little confused about your question. Could you elaborate on the last sentence of your question? Do you want to close default or new Window to keep only one Window? What does default app refer to?

0 Votes 0 ·

@DaisyTian-MSFT

I want to close the already running app instance and and keep only new app instance which is open from browser using custom URI Scheme.

0 Votes 0 ·
Viorel-1 avatar image
1 Vote"
Viorel-1 answered Viorel-1 edited

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

DaisyTian-1203 avatar image
0 Votes"
DaisyTian-1203 answered

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.