11using System ;
2+ using System . Reflection ;
23using System . Runtime . InteropServices ;
34using System . Threading ;
45using Microsoft . UI . Dispatching ;
@@ -26,7 +27,23 @@ private static void Main(string[] args)
2627 try
2728 {
2829 ComWrappersSupport . InitializeComWrappers ( ) ;
29- if ( ! CheckSingleInstance ( ) ) return ;
30+ AppInstance mainInstance = GetMainInstance ( ) ;
31+ if ( ! mainInstance . IsCurrent )
32+ {
33+ var activationArgs = AppInstance . GetCurrent ( ) . GetActivatedEventArgs ( ) ;
34+ mainInstance . RedirectActivationToAsync ( activationArgs ) . AsTask ( ) . Wait ( ) ;
35+ return ;
36+ }
37+
38+ // Register for URI handling (known as "protocol activation")
39+ #if DEBUG
40+ const string scheme = "coder-debug" ;
41+ #else
42+ const string scheme = "coder" ;
43+ #endif
44+ var thisBin = Assembly . GetExecutingAssembly ( ) . Location ;
45+ ActivationRegistrationManager . RegisterForProtocolActivation ( scheme , thisBin + ",1" , "Coder Desktop" , "" ) ;
46+
3047 Application . Start ( p =>
3148 {
3249 var context = new DispatcherQueueSynchronizationContext ( DispatcherQueue . GetForCurrentThread ( ) ) ;
@@ -38,6 +55,9 @@ private static void Main(string[] args)
3855 e . Handled = true ;
3956 ShowExceptionAndCrash ( e . Exception ) ;
4057 } ;
58+
59+ // redirections via RedirectActivationToAsync above get routed to the App
60+ mainInstance . Activated += app . OnActivated ;
4161 } ) ;
4262 }
4363 catch ( Exception e )
@@ -46,20 +66,17 @@ private static void Main(string[] args)
4666 }
4767 }
4868
49- [ STAThread ]
50- private static bool CheckSingleInstance ( )
69+ private static AppInstance GetMainInstance ( )
5170 {
5271#if ! DEBUG
5372 const string appInstanceName = "Coder.Desktop.App" ;
5473#else
5574 const string appInstanceName = "Coder.Desktop.App.Debug" ;
5675#endif
5776
58- var instance = AppInstance . FindOrRegisterForKey ( appInstanceName ) ;
59- return instance . IsCurrent ;
77+ return AppInstance . FindOrRegisterForKey ( appInstanceName ) ;
6078 }
6179
62- [ STAThread ]
6380 private static void ShowExceptionAndCrash ( Exception e )
6481 {
6582 const string title = "Coder Desktop Fatal Error" ;
0 commit comments