%3CLINGO-SUB%20id%3D%22lingo-sub-316581%22%20slang%3D%22en-US%22%3EDesktop%20Bridge%20%E2%80%93%20Multiple%20desktop%20processes%2C%20a%20single%20UWP%20container%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-316581%22%20slang%3D%22en-US%22%3E%0A%20%26lt%3Bmeta%20http-equiv%3D%22Content-Type%22%20content%3D%22text%2Fhtml%3B%20charset%3DUTF-8%22%20%2F%26gt%3B%3CSTRONG%3E%20First%20published%20on%20MSDN%20on%20Nov%2028%2C%202016%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fblogs.msdn.microsoft.com%2Fappconsult%2F2016%2F10%2F31%2Fdesktop-bridge-expanding-a-desktop-application-with-the-uwp-apis%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3EIn%20the%20previous%20post%20%3C%2FA%3E%20we%20have%20learned%20how%20to%20expand%20a%20desktop%20application%20with%20a%20background%20task%2C%20which%20is%20a%20component%20that%20belongs%20to%20the%20Universal%20Windows%20Platform.%20For%20the%20first%20time%2C%20we%20didn%E2%80%99t%20have%20just%20a%20single%20process%20running%20inside%20the%20UWP%20container%20(like%20we%E2%80%99ve%20seen%20%3CA%20href%3D%22https%3A%2F%2Fblogs.msdn.microsoft.com%2Fappconsult%2F2016%2F10%2F31%2Fdesktop-bridge-expanding-a-desktop-application-with-the-uwp-apis%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%20in%20this%20post%20%3C%2FA%3E%20%2C%20where%20we%20were%20leveraging%20some%20UWP%20APIs%20but%2C%20in%20the%20end%2C%20the%20only%20running%20process%20was%20the%20Win32%20one)%2C%20but%20multiple%20ones%3A%20the%20Windows%20Forms%20application%20and%20the%20backgroundTaskHost.exe%20process%2C%20which%20is%20the%20one%20that%20Windows%20uses%20to%20handle%20background%20tasks.%3C%2FP%3E%0A%20%20%3CP%3EThe%20purpose%20of%20this%20post%20is%20to%20help%20you%20to%20better%20understand%2C%20with%20two%20real%20world%20examples%2C%20that%20with%20the%20Desktop%20Bridge%2C%20even%20if%20we%20have%20multiple%20process%20inside%20our%20AppX%2C%20they%E2%80%99re%20actually%20running%20inside%20the%20same%20UWP%20container.%20like%20it%E2%80%99s%20highlighted%20in%20the%20following%20image.%3C%2FP%3E%0A%20%20%3CP%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F66763i4F3F9C2105D46EE1%22%20%2F%3E%3C%2FP%3E%0A%20%20%3CP%3E%3C%2FP%3E%0A%20%20%3CP%3EThe%20main%20benefit%20of%20this%20approach%20is%20that%2C%20even%20if%20we%E2%80%99re%20talking%20about%20separate%20processes%2C%20they%20share%20the%20same%20context%20and%20the%20same%20resources%20and%20they%20can%20easily%20exchange%20data%20between%20them.%20The%20simplest%20and%20most%20common%20example%20is%20the%20local%20storage%3A%20the%20AppData%20area%2C%20where%20desktop%20and%20UWP%20application%20can%20write%20and%20read%20data%2C%20is%20the%20same%2C%20so%20the%20two%20process%20can%20access%20to%20the%20same%20folders%20and%20the%20same%20files.%20Let%E2%80%99s%20make%20a%20real%20example%20by%20expanding%20the%20application%20we%20have%20used%20%3CA%20href%3D%22https%3A%2F%2Fblogs.msdn.microsoft.com%2Fappconsult%2F2016%2F10%2F31%2Fdesktop-bridge-expanding-a-desktop-application-with-the-uwp-apis%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%20in%20the%20previous%20post%20%3C%2FA%3E%20and%20that%20you%20can%20download%20from%20my%20GitHub%20repository%3A%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fqmatteoq%2FDesktopBridge%2Ftree%2Fmaster%2F5.%2520Extend%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%20https%3A%2F%2Fgithub.com%2Fqmatteoq%2FDesktopBridge%2Ftree%2Fmaster%2F5.%2520Extend%3C%2FA%3E%3C%2FP%3E%0A%20%20%3CP%3EThe%20scenario%20described%20in%20the%20post%20was%20leveraging%20two%20different%20processes%3A%3C%2FP%3E%0A%20%20%3CUL%3E%0A%20%20%20%3CLI%3EA%20Win32%20one%2C%20which%20is%20a%20Windows%20Forms%20application%20that%2C%20by%20leveraging%20a%20set%20of%20UWP%20APIs%2C%20is%20registering%20a%20background%20task%20connected%20to%20the%20%3CSTRONG%3E%20TimeZoneTrigger%20%3C%2FSTRONG%3E%20trigger.%3C%2FLI%3E%0A%20%20%20%3CLI%3EA%20UWP%20one%2C%20which%20is%20a%20Windows%20Runtime%20Component%20that%20takes%20care%20of%20updating%20the%20tile%20of%20the%20application%20and%20to%20show%20a%20toast%20notifications.%3C%2FLI%3E%0A%20%20%3C%2FUL%3E%0A%20%20%3CP%3EBy%20combining%20together%20the%20two%20processes%20we%20created%20a%20desktop%20application%20that%2C%20by%20registering%20the%20background%20task%2C%20was%20able%20to%20send%20a%20notification%20to%20the%20user%20and%20to%20update%20its%20tile%20every%20time%20the%20user%20changed%20the%20time%20zone%20of%20his%20computer%20in%20the%20Windows%E2%80%99%20settings.%20In%20this%20post%20we%E2%80%99re%20going%20to%20expand%20the%20scenario%20by%20adding%20the%20following%20features%2C%20which%20will%20be%20made%20possible%20by%20the%20fact%20that%20the%20two%20applications%20are%20running%20inside%20the%20same%20container%3A%3C%2FP%3E%0A%20%20%3COL%3E%0A%20%20%20%3CLI%3EIn%20the%20Windows%20Forms%20application%2C%20we%E2%80%99re%20going%20to%20add%20a%20TextBox%20control%2C%20where%20the%20user%20will%20have%20the%20chance%20to%20write%20a%20custom%20message.%3C%2FLI%3E%0A%20%20%20%3CLI%3EBy%20using%20the%20UWP%20APIs%20to%20handle%20settings%2C%20we%E2%80%99re%20going%20to%20save%20this%20text%20inside%20the%20local%20storage.%3C%2FLI%3E%0A%20%20%20%3CLI%3EThe%20background%20task%2C%20before%20running%2C%20will%20try%20to%20load%20the%20data%20from%20the%20settings%20and%20it%20will%20include%20the%20custom%20message%20inside%20the%20text%20displayed%20in%20the%20live%20tile%20and%20in%20the%20toast%20notification%20sent%20to%20the%20user.%3C%2FLI%3E%0A%20%20%3C%2FOL%3E%0A%20%20%3CH3%20id%3D%22toc-hId-1477804117%22%20id%3D%22toc-hId-1505537367%22%20id%3D%22toc-hId-1505537367%22%3EThe%20Windows%20Forms%20application%3C%2FH3%3E%0A%20%20%3CP%3EThe%20work%20to%20be%20done%20in%20the%20Windows%20Forms%20application%20is%20very%20simple%3A%20first%2C%20we%20need%20to%20add%2C%20above%20the%20button%20we%20have%20included%20%3CA%20href%3D%22https%3A%2F%2Fblogs.msdn.microsoft.com%2Fappconsult%2F2016%2F10%2F31%2Fdesktop-bridge-expanding-a-desktop-application-with-the-uwp-apis%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%20in%20the%20previous%20post%20%3C%2FA%3E%20to%20register%20the%20background%20task%2C%20a%20%3CSTRONG%3E%20TextBox%20%3C%2FSTRONG%3E%20control%2C%20where%20the%20user%20will%20be%20able%20to%20write%20his%20custom%20message.%20Then%2C%20we%20need%20to%20change%20a%20little%20bit%20the%20code%20that%20is%20executed%20when%20the%20user%20presses%20the%20button%3A%3C%2FP%3Eprivate%20async%20void%20OnRegisterTask(object%20sender%2C%20EventArgs%20e)%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20if%20(!string.IsNullOrEmpty(txtMessage.Text))%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20ApplicationData.Current.LocalSettings.Values.Clear()%3B%20%3CBR%20%2F%3E%20ApplicationData.Current.LocalSettings.Values.Add(%22message%22%2C%20txtMessage.Text)%3B%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20string%20triggerName%20%3D%20%22TimeZoneTriggerTest%22%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%2F%2F%20Check%20if%20the%20task%20is%20already%20registered%20%3CBR%20%2F%3E%20foreach%20(var%20cur%20in%20BackgroundTaskRegistration.AllTasks)%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20if%20(cur.Value.Name%20%3D%3D%20triggerName)%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20%2F%2F%20The%20task%20is%20already%20registered.%20%3CBR%20%2F%3E%20return%3B%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20BackgroundTaskBuilder%20builder%20%3D%20new%20BackgroundTaskBuilder()%3B%20%3CBR%20%2F%3E%20builder.Name%20%3D%20triggerName%3B%20%3CBR%20%2F%3E%20builder.SetTrigger(new%20SystemTrigger(SystemTriggerType.TimeZoneChange%2C%20false))%3B%20%3CBR%20%2F%3E%20builder.TaskEntryPoint%20%3D%20%22TileBackgroundTask.TileTask%22%3B%20%3CBR%20%2F%3E%20var%20status%20%3D%20await%20BackgroundExecutionManager.RequestAccessAsync()%3B%20%3CBR%20%2F%3E%20if%20(status%20!%3D%20BackgroundAccessStatus.DeniedByUser%20%26amp%3B%26amp%3B%20status%20!%3D%20BackgroundAccessStatus.DeniedBySystemPolicy)%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20builder.Register()%3B%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CP%3EWe%20can%20spot%20the%20difference%20compared%20to%20the%20code%20we%20have%20seen%20in%20the%20previous%20post%20in%20the%20first%20lines%20of%20the%20event%20handler%3A%20if%20the%20%3CSTRONG%3E%20Text%20%3C%2FSTRONG%3E%20property%20of%20the%20%3CSTRONG%3E%20TextBox%20%3C%2FSTRONG%3E%20control%20we%20have%20added%20(identified%20by%20the%20name%20%3CSTRONG%3E%20txtMessage%20%3C%2FSTRONG%3E%20)%20contains%20a%20value%2C%20we%20save%20it%20in%20the%20local%20storage%20using%20the%20settings%20APIs.%20These%20APIs%20are%20very%20simple%20to%20use%3A%20by%20using%20the%20static%20class%20%3CSTRONG%3E%20ApplicationData.Current.LocalSettings%20%3C%2FSTRONG%3E%20we%20can%20access%20to%20a%20collection%20called%20%3CSTRONG%3E%20Values%20%3C%2FSTRONG%3E%20%2C%20which%20is%20a%20dictionary%20where%20we%20can%20store%20a%20set%20of%20key%20%2F%20value%20pairs.%20In%20this%20case%2C%20we%20store%20an%20element%20identified%20by%20the%20key%20%3CSTRONG%3E%20message%20%3C%2FSTRONG%3E%20which%20contains%2C%20as%20value%2C%20the%20custom%20message%20written%20by%20the%20user.%20The%20rest%20of%20the%20code%20is%20the%20same%20we%E2%80%99ve%20seen%20in%20the%20previous%20post%20and%20it%E2%80%99s%20the%20one%20that%20takes%20care%20of%20registering%20the%20background%20task%20in%20the%20proper%20way.%3C%2FP%3E%3CBR%20%2F%3E%3CH3%20id%3D%22toc-hId--1074352844%22%20id%3D%22toc-hId--1046619594%22%20id%3D%22toc-hId--1046619594%22%3EThe%20background%20task%3C%2FH3%3E%3CBR%20%2F%3E%3CP%3EInside%20the%20background%20task%20we%E2%80%99re%20going%20to%20perform%20the%20reverse%20operation%3A%20we%E2%80%99re%20going%20to%20check%20if%2C%20in%20the%20settings%2C%20there%E2%80%99s%20a%20value%20identified%20by%20the%20key%20called%20%3CSTRONG%3E%20message%20%3C%2FSTRONG%3E%20and%2C%20if%20it%E2%80%99s%20positive%2C%20we%20read%20it%20as%20a%20string.%20Here%20is%20the%20new%20implementation%20of%20the%20%3CSTRONG%3E%20Run()%20%3C%2FSTRONG%3E%20method%20%3CSTRONG%3E%20%3C%2FSTRONG%3E%20inside%20the%20%3CSTRONG%3E%20TileTask%20%3C%2FSTRONG%3E%20of%20the%20Windows%20Runtime%20Component%3A%3C%2FP%3Epublic%20sealed%20class%20TileTask%20%3A%20IBackgroundTask%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20public%20void%20Run(IBackgroundTaskInstance%20taskInstance)%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20string%20message%20%3D%20string.Empty%3B%20%3CBR%20%2F%3E%20if%20(ApplicationData.Current.LocalSettings.Values.ContainsKey(%22message%22))%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20message%20%3D%20ApplicationData.Current.LocalSettings.Values%5B%22message%22%5D.ToString()%3B%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20string%20tileXml%20%3D%20%24%40%22%3CTILE%3E%20%3CBR%20%2F%3E%20%3CVISUAL%3E%20%3CBR%20%2F%3E%20%3CBINDING%20template%3D%22'TileMedium'%22%20branding%3D%22'logo'%22%3E%20%3CBR%20%2F%3E%20%3CGROUP%3E%20%3CBR%20%2F%3E%20%3CSUBGROUP%3E%20%3CBR%20%2F%3E%20%3CTEXT%20hint-style%3D%22'caption'%22%3ETime%20zone%20changed!%3C%2FTEXT%3E%20%3CBR%20%2F%3E%20%3CTEXT%20hint-style%3D%22'captionSubtle'%22%20hint-wrap%3D%22'true'%22%3E%7Bmessage%7D%3C%2FTEXT%3E%20%3CBR%20%2F%3E%20%3CTEXT%20hint-style%3D%22'captionSubtle'%22%20hint-wrap%3D%22'true'%22%3ELast%20update%20at%20%7BDateTime.Now%7D%3C%2FTEXT%3E%20%3CBR%20%2F%3E%20%3C%2FSUBGROUP%3E%20%3CBR%20%2F%3E%20%3C%2FGROUP%3E%20%3CBR%20%2F%3E%20%3C%2FBINDING%3E%20%3CBR%20%2F%3E%20%3C%2FVISUAL%3E%20%3CBR%20%2F%3E%20%3C%2FTILE%3E%22%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20XmlDocument%20tileDoc%20%3D%20new%20XmlDocument()%3B%20%3CBR%20%2F%3E%20tileDoc.LoadXml(tileXml)%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20TileNotification%20notification%20%3D%20new%20TileNotification(tileDoc)%3B%20%3CBR%20%2F%3E%20TileUpdateManager.CreateTileUpdaterForApplication().Update(notification)%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20string%20toastXml%20%3D%20%24%40%22%3CTOAST%3E%20%3CBR%20%2F%3E%20%3CVISUAL%3E%20%3CBR%20%2F%3E%20%3CBINDING%20template%3D%22'ToastGeneric'%22%3E%20%3CBR%20%2F%3E%20%3CTEXT%3ETime%20zone%20changed!%3C%2FTEXT%3E%20%3CBR%20%2F%3E%20%3CTEXT%3E%7Bmessage%7D%3C%2FTEXT%3E%20%3CBR%20%2F%3E%20%3CTEXT%3ELast%20update%20at%20%7BDateTime.Now%7D%3C%2FTEXT%3E%20%3CBR%20%2F%3E%20%3C%2FBINDING%3E%20%3CBR%20%2F%3E%20%3C%2FVISUAL%3E%20%3CBR%20%2F%3E%20%3C%2FTOAST%3E%22%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20XmlDocument%20toastDoc%20%3D%20new%20XmlDocument()%3B%20%3CBR%20%2F%3E%20toastDoc.LoadXml(toastXml)%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20ToastNotification%20toast%20%3D%20new%20ToastNotification(toastDoc)%3B%20%3CBR%20%2F%3E%20ToastNotificationManager.CreateToastNotifier().Show(toast)%3B%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CP%3EYou%20can%20notice%20that%20we%20are%20using%20the%20same%20APIs%20that%20we%20have%20used%20in%20the%20desktop%20application.%20The%20only%20difference%20is%20that%2C%20before%20using%20the%20%3CSTRONG%3E%20Applicationdata.Current.LocalSettings.Values%20%3C%2FSTRONG%3E%20collection%20to%20retrieve%20the%20value%20identified%20by%20the%20%3CSTRONG%3E%20message%20%3C%2FSTRONG%3E%20key%2C%20we%20use%20the%20%3CSTRONG%3E%20ContainsKey()%20%3C%2FSTRONG%3E%20method%20to%20make%20sure%20that%20such%20value%20really%20exists.%20If%20it%E2%80%99s%20positive%2C%20we%20retrieve%20it%20and%20we%20store%20it%20inside%20a%20variable.%20The%20rest%20of%20the%20code%20is%20the%20same%20we%E2%80%99ve%20seen%20%3CA%20href%3D%22https%3A%2F%2Fblogs.msdn.microsoft.com%2Fappconsult%2F2016%2F10%2F31%2Fdesktop-bridge-expanding-a-desktop-application-with-the-uwp-apis%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%20in%20the%20previous%20post%20%3C%2FA%3E%20%2C%20with%20just%20one%20difference%3A%20in%20the%20XML%20payload%20that%20describes%20the%20tile%20and%20the%20toast%20notification%20we%20have%20added%20a%20new%20%3CSTRONG%3E%20text%20%3C%2FSTRONG%3E%20element%2C%20which%20contains%20the%20message%20retrieved%20from%20the%20settings.%3C%2FP%3E%3CBR%20%2F%3E%3CH3%20id%3D%22toc-hId-668457491%22%20id%3D%22toc-hId-696190741%22%20id%3D%22toc-hId-696190741%22%3EIt%E2%80%99s%20testing%20time!%3C%2FH3%3E%3CBR%20%2F%3E%3CP%3EOur%20job%20is%20completed%3A%20now%20compile%20again%20the%20solution%2C%20launch%20the%20UWP%20deployment%20project%20included%20in%20your%20Visual%20Studio%2017%20RC%E2%80%99s%20solution%20and%2C%20this%20time%2C%20before%20pressing%20the%20button%20to%20register%20the%20task%2C%20write%20a%20message%20inside%20the%20TextBox.%20Now%20repeat%20the%20test%20we%20made%20in%20the%20previous%20post%3A%20open%20the%20Windows%E2%80%99%20settings%2C%20choose%20%3CSTRONG%3E%20Time%20%26amp%3B%20Language%20%E2%80%93%26gt%3B%20Date%20%26amp%3B%20Time%20%3C%2FSTRONG%3E%20and%20change%20the%20current%20time%20zone.%20You%20will%20notice%20that%2C%20this%20time%2C%20the%20tile%20and%20toast%20notifications%20will%20contain%2C%20other%20than%20the%20title%20and%20the%20date%20and%20time%20of%20the%20last%20update%2C%20also%20the%20custom%20message%20we%20have%20added%20in%20the%20Windows%20Forms%20application%20(in%20the%20below%20image%2C%20it%E2%80%99s%20the%20text%20%3CSTRONG%3E%20Hello%20Desktop%20Bridge!%20%3C%2FSTRONG%3E%20%3A(%3C%2Fimg%3E%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F66764i1903491F847CD70F%22%20%2F%3E%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%3C%2FP%3E%3CBR%20%2F%3E%3CH3%20id%3D%22toc-hId--1883699470%22%20id%3D%22toc-hId--1855966220%22%20id%3D%22toc-hId--1855966220%22%3EUsing%20the%20system%20registry%3C%2FH3%3E%3CBR%20%2F%3E%3CP%3ELet%E2%80%99s%20see%20another%20example%20of%20how%20multiple%20processes%20running%20inside%20the%20same%20UWP%20container%20share%20the%20same%20resources.%20This%20time%2C%20we%E2%80%99re%20going%20to%20use%20a%20more%20traditional%20approach%3A%20no%20more%20UWP%20APIs%2C%20but%20we%E2%80%99re%20going%20to%20use%20only%20standard%20.NET%20Framework%20APIs.%20As%20we%20have%20learned%20%3CA%20href%3D%22https%3A%2F%2Fblogs.msdn.microsoft.com%2Fappconsult%2F2016%2F10%2F13%2Fdesktop-bridge-the-bridge-between-win32-apps-and-the-universal-windows-platform%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%20in%20the%20first%20post%20of%20the%20series%20%3C%2FA%3E%20%2C%20applications%20converted%20using%20the%20Desktop%20Bridge%20run%20inside%20a%20container%2C%20which%20we%20can%20consider%20as%20a%20virtualized%20environment%3A%20the%20file%20system%20and%20the%20registry%20will%20be%20isolated%20and%20merged%2C%20in%20real%20time%2C%20with%20the%20system%20ones.%20This%20way%2C%20the%20application%20will%20continue%20to%20have%20complete%20access%20to%20the%20registry%20and%20work%20as%20expected%20but%2C%20when%20uninstalled%2C%20it%20won%E2%80%99t%20be%20able%20to%20leave%20any%20leftover%20that%2C%20during%20time%2C%20can%20lead%20to%20slow%20down%20the%20operating%20system.%20This%20is%20made%20possible%20by%20the%20fact%20that%2C%20when%20a%20converted%20application%20tries%20to%20write%20some%20data%20in%20the%20registry%2C%20the%20operation%20won%E2%80%99t%20be%20performed%20against%20the%20system%20registry%2C%20but%20against%20a%20virtualized%20version%20(which%20is%20simply%20a%20special%20text%20file).%20Let%E2%80%99s%20see%20a%20real%20example%20to%20better%20understand%20this%20concept%2C%20by%20creating%20a%20new%20Visual%20Studio%20solution%3A%20also%20in%20this%20case%2C%20to%20make%20our%20life%20easier%2C%20we%E2%80%99re%20going%20to%20use%20Visual%20Studio%202017%20RC%20and%20the%20UWP%20deployment%20extension.%20%3CBR%20%2F%3E%3C%2FP%3E%0A%20%20%3CP%3ELet%E2%80%99s%20start%20by%20creating%20a%20Windows%20Forms%20application%20with%20a%20%3CSTRONG%3E%20Button%20%3C%2FSTRONG%3E%20control%3A%20when%20it%E2%80%99s%20pressed%2C%20we%E2%80%99re%20going%20to%20use%20the%20standard%20.NET%20Framework%20APIs%20%3CSTRONG%3E%20%3C%2FSTRONG%3E%20to%20create%20a%20new%20registry%20key.%3C%2FP%3Eprivate%20void%20OnCreateRegistryKey(Object%20sender%2C%20EventArgs%20e)%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20var%20RegKey%20%3D%20Registry.CurrentUser.OpenSubKey(%40%22SOFTWARE%5CMatteo%20Pagani%5CDesktopBridge%22%2C%20true)%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20if%20(RegKey%20%3D%3D%20null)%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20RegKey%20%3D%20Registry.CurrentUser.CreateSubKey(%40%22SOFTWARE%5CMatteo%20Pagani%5CDesktopBridge%22%2C%20RegistryKeyPermissionCheck.ReadWriteSubTree)%3B%20%3CBR%20%2F%3E%20RegKey.SetValue(%22IsAppInstalled%22%2C%20true)%3B%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CP%3EThe%20code%20uses%20the%20%3CSTRONG%3E%20Registry%20%3C%2FSTRONG%3E%20class%20and%20the%20%3CSTRONG%3E%20OpenSubKey()%20%3C%2FSTRONG%3E%20method%20to%20access%20to%20a%20specific%20registry%20key%20inside%20the%20hive%20of%20the%20current%20user%20(this%20way%2C%20the%20application%20will%20be%20able%20to%20run%20without%20requiring%20admin%20rights%2C%20which%20is%20a%20scenario%20not%20supported%20by%20converted%20apps).%20The%20key%20is%20called%20%3CSTRONG%3E%20IsAppInstalled%20%3C%2FSTRONG%3E%20and%20it%E2%80%99s%20stored%20inside%20the%20path%20%3CSTRONG%3E%20HKEY_CURRENT_USER%5CSOFTWARE%5CMatteo%20Pagani%5CDesktopBridge.%20%3C%2FSTRONG%3E%20If%20the%20key%20doesn%E2%80%99t%20exist%2C%20we%20create%20it%20by%20leveraging%20the%20%3CSTRONG%3E%20SetValue()%20%3C%2FSTRONG%3E%20method%2C%20passing%20as%20parameters%20the%20name%20of%20the%20key%20and%20its%20value%20(%20%3CSTRONG%3E%20true%20%3C%2FSTRONG%3E%20).%20%3CBR%20%2F%3E%3C%2FP%3E%0A%20%20%3CP%3ELet%E2%80%99s%20start%2C%20as%20a%20first%20test%2C%20to%20launch%20the%20application%20as%20a%20native%20Win32%20process%3A%20let%E2%80%99s%20click%20on%20the%20button%2C%20then%20open%20the%20system%20registry%20(by%20opening%20the%20Start%20menu%2C%20writing%20%3CSTRONG%3E%20regedit%20%3C%2FSTRONG%3E%20and%20pressing%20Enter).%20If%20we%20did%20everything%20correctly%2C%20we%20will%20find%20the%20key%20we%20have%20just%20created%3A%20%3CBR%20%2F%3E%3C%2FP%3E%0A%20%20%3CP%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F66765iC48CD133013BD45E%22%20%2F%3E%20%3CBR%20%2F%3E%3C%2FP%3E%0A%20%20%3CP%3ENow%2C%20from%20the%20registry%20editor%2C%20delete%20the%20key%20we%20have%20just%20created%20(in%20my%20case%2C%20the%20entire%20folder%20called%20%3CSTRONG%3E%20Matteo%20Pagani%20%3C%2FSTRONG%3E%20)%20and%20now%20move%20on%20to%20the%20next%20step%2C%20which%20is%20adding%20a%20Desktop%20to%20UWP%20deployment%20project%20to%20our%20solution.%20Let%E2%80%99s%20configure%20it%20in%20a%20way%20that%20we%20will%20be%20able%20to%20launch%20the%20Windows%20Forms%20application%20we%20have%20just%20created%20inside%20the%20UWP%20container%20(I%20won%E2%80%99t%20repeat%20in%20this%20post%20all%20the%20required%20steps%2C%20since%20I%E2%80%99ve%20already%20explained%20this%20approach%20%3CA%20href%3D%22https%3A%2F%2Fblogs.msdn.microsoft.com%2Fappconsult%2F2016%2F10%2F28%2Fdesktop-bridge-manually-converting-a-desktop-application%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%20in%20one%20of%20the%20previous%20posts%20%3C%2FA%3E%20)%2C%20then%20let%E2%80%99s%20try%20to%20launch%20it.%20The%20operation%20will%20complete%20just%20fine%20but%2C%20this%20time%2C%20if%20we%20repeat%20the%20previous%20steps%20to%20look%20for%20the%20%3CSTRONG%3E%20HKEY_CURRENT_USER%5CSOFTWARE%5CMatteo%20Pagani%5CDesktopBridge%20%3C%2FSTRONG%3E%20path%20in%20the%20registry%20editor%2C%20we%20won%E2%80%99t%20be%20able%20to%20find%20it.%20The%20reason%20is%20that%2C%20this%20time%2C%20the%20application%20isn%E2%80%99t%20running%20as%20a%20native%20process%20but%20inside%20the%20UWP%20container%20and%2C%20consequently%2C%20it%20isn%E2%80%99t%20writing%20anymore%20inside%20the%20system%20registry%2C%20but%20inside%20the%20virtualized%20version%20offered%20by%20the%20container.%20%3CBR%20%2F%3E%3C%2FP%3E%0A%20%20%3CP%3EThere%E2%80%99s%20an%20important%20takeaway%20that%20we%20can%20learn%20from%20this%20test%3A%20when%20it%20comes%20to%20converted%20applications%2C%20we%20can%E2%80%99t%20use%20the%20registry%20as%20a%20way%20to%20handle%20the%20communication%20between%20two%20different%20application%20if%20they%20aren%E2%80%99t%20running%20inside%20the%20same%20UWP%20container.%20We%20can%20easily%20see%20this%20blocker%20by%20adding%2C%20to%20our%20solution%2C%20a%20new%20Windows%20Forms%20project%20with%20another%20button%20that%2C%20this%20time%2C%20it%20will%20read%20the%20registry%20key%20instead%20of%20writing%20it%3A%3C%2FP%3Eprivate%20void%20OnReadRegistryKey(Object%20sender%2C%20EventArgs%20e)%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20bool%20isAppInstalled%20%3D%20false%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20var%20RegKey%20%3D%20Registry.CurrentUser.OpenSubKey(%40%22SOFTWARE%5CMatteo%20Pagani%5CDesktopBridge%22%2C%20true)%3B%20%3CBR%20%2F%3E%20if%20(RegKey%20!%3D%20null)%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20string%20result%20%3D%20RegKey.GetValue(%22IsAppInstalled%22).ToString()%3B%20%3CBR%20%2F%3E%20isAppInstalled%20%3D%20result%20%3D%3D%20%22True%22%20%3F%20true%20%3A%20false%3B%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20string%20message%20%3D%20isAppInstalled%20%3F%20%22The%20app%20is%20installed%22%20%3A%20%22The%20app%20isn't%20installed%22%3B%20%3CBR%20%2F%3E%20MessageBox.Show(message)%3B%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CP%3EBy%20using%20the%20same%20APIs%20we%20have%20learned%20in%20the%20previous%20sample%20(the%20%3CSTRONG%3E%20Registry.CurrentUser%20%3C%2FSTRONG%3E%20class%20and%20the%20%3CSTRONG%3E%20OpenSubKey()%20%3C%2FSTRONG%3E%20method)%2C%20we%20check%20if%20the%20%3CSTRONG%3E%20HKEY_CURRENT_USER%5CSOFTWARE%5CMatteo%20Pagani%5CDesktopBridge%20%3C%2FSTRONG%3E%20path%20exists.%20If%20it%E2%80%99s%20positive%2C%20we%20check%20if%20the%20key%20identified%20by%20the%20name%20%3CSTRONG%3E%20IsAppInstalled%20%3C%2FSTRONG%3E%20exists%20and%20we%20display%20a%20message%20to%20the%20user%2C%20based%20on%20the%20outcome%20of%20the%20operation.%20%3CBR%20%2F%3E%3C%2FP%3E%0A%20%20%3CP%3EIf%20we%20now%20try%20to%20launch%20both%20application%20as%20native%20(so%20without%20using%20the%20UWP%20container)%20and%20first%20we%20create%20the%20registry%20key%20using%20the%20first%20app%2C%20then%20we%20read%20it%20using%20the%20second%20app%2C%20everything%20will%20work%20as%20expected%3A%20we%20will%20see%20the%20message%20that%20the%20key%20has%20been%20found%2C%20since%20both%20applications%20are%20writing%20and%20reading%20from%20the%20system%20registry.%20However%2C%20if%20we%20try%20to%20use%20the%20UWP%20deployment%20project%20to%20launch%20the%20first%20app%20inside%20the%20UWP%20container%20and%20the%20second%20app%2C%20instead%2C%20as%20native%2C%20we%20will%20get%20an%20error%20message%3A%20the%20key%20doesn%E2%80%99t%20exist.%20The%20reason%20is%20that%20that%20the%20first%20application%2C%20since%20it%20runs%20inside%20the%20container%2C%20has%20written%20the%20key%20inside%20the%20virtualized%20registry%2C%20while%20the%20second%20application%20is%20running%20as%20native%20and%2C%20as%20such%2C%20it%E2%80%99s%20looking%20for%20it%20in%20the%20system%20registry.%20%3CBR%20%2F%3E%3C%2FP%3E%0A%20%20%3CP%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F66766i39FFA5536DD8BBEC%22%20%2F%3E%20%3CBR%20%2F%3E%3C%2FP%3E%0A%20%20%3CP%3EHow%20to%20handle%20these%20kind%20of%20scenarios%3F%20The%20best%20answer%20would%20be%20to%20start%20using%20a%20different%20approach%20and%20start%20leveraging%20some%20features%20offered%20by%20the%20Universal%20Windows%20Platform%2C%20like%20App%20Services%20or%20the%20new%20kind%20of%20shared%20storage%20introduced%20in%20the%20Anniversary%20Update%20that%20allows%20to%20read%20and%20write%20files%20by%20applications%20developed%20by%20the%20same%20publisher.%20However%2C%20we%20don%E2%80%99t%20always%20have%20the%20opportunity%20to%20refactor%20our%20application%20in%20the%20short%20term%2C%20because%20we%20may%20have%20some%20business%20and%20timing%20requirements%20to%20respect.%20As%20such%2C%20another%20approach%20we%20can%20leverage%20that%20doesn%E2%80%99t%20require%20us%20to%20partially%20rewrite%20our%20application%20is%20the%20same%20we%E2%80%99ve%20seen%20at%20the%20beginning%20of%20this%20post%3A%20when%20two%20or%20more%20processes%20are%20running%20inside%20the%20same%20container%2C%20they%20share%20the%20same%20resources.%20In%20the%20first%20example%20of%20the%20post%2C%20the%20resource%20that%20we%20shared%20was%20the%20local%20storage%2C%20but%20also%20the%20registry%20is%20one%20of%20these%20resources.%20As%20such%2C%20if%20we%20include%20all%20our%20Win32%20processes%20inside%20the%20same%20AppX%20package%2C%20they%20will%20be%20able%20to%20read%20and%20write%20from%20the%20same%20virtualized%20registry%2C%20exactly%20in%20the%20same%20way%20in%20the%20previous%20example%20the%20Windows%20Forms%20application%20and%20the%20background%20task%20were%20able%20to%20read%20and%20write%20from%20the%20same%20local%20storage%2C%20since%20they%20were%20both%20stored%20in%20the%20same%20AppX%20package.%20%3CBR%20%2F%3E%3C%2FP%3E%0A%20%20%3CP%3EThe%20first%20step%20to%20achieve%20this%20goal%20is%20to%20change%20the%20deployment%20project%20so%20that%2C%20when%20we%20deploy%20it%2C%20it%20will%20include%20both%20the%20Windows%20Forms%20applications%20and%20not%20just%20the%20original%20one%20we%20have%20created.%20Here%20is%20how%20we%20can%20configure%20the%20%3CSTRONG%3E%20AppXPackageFileList.xml%20%3C%2FSTRONG%3E%20file%20so%20that%20it%20can%20retrieve%2C%20from%20the%20Visual%20Studio%20output%20folders%2C%20the%20executables%20of%20the%20two%20desktop%20applications%20and%20copy%20them%20inside%20the%20%3CSTRONG%3E%20PackageLayout%20%3C%2FSTRONG%3E%20folder%3A%3C%2FP%3E%0A%20%20%20%3CBR%20%2F%3E%20%3CPROJECT%20toolsversion%3D%22%26quot%3B14.0%26quot%3B%22%20xmlns%3D%22%26quot%3B%26lt%3BA%22%20href%3D%22http%3A%2F%2Fschemas.microsoft.com%2Fdeveloper%2Fmsbuild%2F2003%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3E%3CA%20href%3D%22http%3A%2F%2Fschemas.microsoft.com%2Fdeveloper%2Fmsbuild%2F2003%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttp%3A%2F%2Fschemas.microsoft.com%2Fdeveloper%2Fmsbuild%2F2003%3C%2FA%3E%22%26gt%3B%20%3CBR%20%2F%3E%20%3CPROPERTYGROUP%3E%20%3CBR%20%2F%3E%20%3CMYPROJECTOUTPUTPATH%3E%24(PackageLayout)%5C..%5C..%5C..%5CRegistry%3C%2FMYPROJECTOUTPUTPATH%3E%20%3CBR%20%2F%3E%20%3C%2FPROPERTYGROUP%3E%20%3CBR%20%2F%3E%20%3CITEMGROUP%3E%20%3CBR%20%2F%3E%20%3CLAYOUTFILE%20include%3D%22%26quot%3B%24(MyProjectOutputPath)%5CDesktopApp.WriteRegistry%5Cbin%5CDebug%5CDesktopApp.WriteRegistry.exe%26quot%3B%22%3E%20%3CBR%20%2F%3E%20%3CPACKAGEPATH%3E%24(PackageLayout)%5CDesktopApp.WriteRegistry.exe%3C%2FPACKAGEPATH%3E%20%3CBR%20%2F%3E%20%3C%2FLAYOUTFILE%3E%20%3CBR%20%2F%3E%20%3CLAYOUTFILE%20include%3D%22%26quot%3B%24(MyProjectOutputPath)%5CDesktopApp.ReadRegistry%5Cbin%5CDebug%5CDesktopApp.ReadRegistry.exe%26quot%3B%22%3E%20%3CBR%20%2F%3E%20%3CPACKAGEPATH%3E%24(PackageLayout)%5CDesktopApp.ReadRegistry.exe%3C%2FPACKAGEPATH%3E%20%3CBR%20%2F%3E%20%3C%2FLAYOUTFILE%3E%20%3CBR%20%2F%3E%20%3C%2FITEMGROUP%3E%20%3CBR%20%2F%3E%20%3C%2FPROJECT%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CP%3EThe%20approach%20is%20similar%20to%20the%20one%20we%E2%80%99ve%20seen%20in%20%3CA%20href%3D%22https%3A%2F%2Fblogs.msdn.microsoft.com%2Fappconsult%2F2016%2F10%2F28%2Fdesktop-bridge-manually-converting-a-desktop-application%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%20one%20of%20the%20previous%20posts%20%3C%2FA%3E%20%2C%20with%20the%20difference%20that%20in%20the%20previous%20sample%20we%20were%20including%20in%20the%20same%20package%20an%20executable%20(a%20Windows%20Forms%20application)%20and%20a%20DLL%20library%20(JSON.NET).%20This%20time%2C%20instead%2C%20we%20include%20two%20executable%3A%20the%20two%20Windows%20Forms%20apps%20we%E2%80%99ve%20created%20before%20(the%20first%20one%20that%20creates%20the%20registry%20key%20and%20the%20second%20one%20that%20reads%20the%20registry%20key).%20Since%20now%20both%20applications%20are%20part%20of%20the%20same%20package%2C%20they%20have%20access%20to%20the%20same%20resources%2C%20including%20the%20virtualized%20version%20of%20the%20registry.%20Let%E2%80%99s%20do%20a%20concrete%20example%20by%20adding%20a%20new%20button%20in%20the%20first%20application%20(the%20one%20called%20%3CSTRONG%3E%20DesktopApp.WriteRegisty%20%3C%2FSTRONG%3E%20)%20that%20will%20invoke%20the%20following%20code%3A%3C%2FP%3Eprivate%20void%20OnOpenReadApp(Object%20sender%2C%20EventArgs%20e)%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20string%20result%20%3D%20System.Reflection.Assembly.GetExecutingAssembly().Location%3B%20%3CBR%20%2F%3E%20int%20index%20%3D%20result.LastIndexOf(%22%5C%5C%22)%3B%20%3CBR%20%2F%3E%20string%20processPath%20%3D%20%24%22%7Bresult.Substring(0%2C%20index)%7D%5C%5CDesktopApp.ReadRegistry.exe%22%3B%20%3CBR%20%2F%3E%20Process.Start(processPath)%3B%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CP%3EThe%20previous%20code%20uses%20the%20.NET%20Framework%20APIs%20to%20retrieve%2C%20by%20using%20the%20reflection%2C%20the%20path%20of%20the%20running%20executable%2C%20so%20that%20it%20composes%20the%20proper%20path%20of%20the%20other%20executable%20which%2C%20instead%2C%20is%20called%20%3CSTRONG%3E%20DesktopApp.ReadRegistry.exe%20%3C%2FSTRONG%3E%20.%20The%20reason%20why%20we%20used%20the%20reflection%20approach%20and%20not%20the%20specific%20API%20to%20retrieve%20the%20current%20working%20directory%20(the%20method%20%3CSTRONG%3E%20Directory.GetCurrentDirectory()%20%3C%2FSTRONG%3E%20)%20is%20that%2C%20when%20a%20desktop%20application%20is%20running%20inside%20the%20UWP%20container%2C%20this%20API%20doesn%E2%80%99t%20return%20the%20real%20current%20directory%2C%20but%20the%20Windows%E2%80%99%20system%20one.%20Consequently%2C%20this%20is%20one%20of%20the%20possible%20workarounds%20we%20can%20use%20to%20get%20access%20to%20the%20path%20of%20the%20other%20executable%20stored%20inside%20the%20same%20package.%20Once%20we%20have%20the%20full%20path%2C%20we%20can%20launch%20the%20second%20application%20by%20calling%20the%20%3CSTRONG%3E%20Process.Start()%20%3C%2FSTRONG%3E%20method.%20%3CBR%20%2F%3E%3C%2FP%3E%0A%20%20%3CP%3EAt%20this%20point%2C%20the%20second%20application%20will%20start%3A%20this%20time%2C%20if%20we%20create%20first%20the%20registry%20key%20using%20the%20button%20in%20the%20first%20app%20and%20then%2C%20in%20the%20second%20app%2C%20we%20press%20the%20button%20to%20read%20the%20same%20key%2C%20the%20operation%20will%20complete%20with%20success.%20The%20reason%20is%20that%20both%20applications%20are%20running%20inside%20the%20same%20UWP%20container%20and%2C%20as%20such%2C%20they%20are%20both%20reading%20and%20writing%20not%20only%20inside%20the%20same%20file%20system%20(like%20we%E2%80%99ve%20seen%20with%20the%20first%20sample)%2C%20but%20also%20inside%20the%20same%20registry.%20%3CBR%20%2F%3E%3C%2FP%3E%0A%20%20%3CP%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F66768i4FA766B79C5EEE23%22%20%2F%3E%20%3CBR%20%2F%3E%3C%2FP%3E%0A%20%20%3CH3%20id%3D%22toc-hId--140889135%22%20id%3D%22toc-hId--113155885%22%20id%3D%22toc-hId--113155885%22%3E%3C%2FH3%3E%3CBR%20%2F%3E%3CH3%20id%3D%22toc-hId-1601921200%22%20id%3D%22toc-hId-1629654450%22%20id%3D%22toc-hId-1629654450%22%3EWrapping%20up%3C%2FH3%3E%3CBR%20%2F%3E%3CP%3EIn%20this%20post%20we%20have%20learned%20more%20about%20how%20the%20UWP%20container%20works%20and%20how%20we%20can%20share%20data%20between%20two%20different%20processes%20running%20in%20the%20same%20container.%20In%20the%20first%20sample%2C%20we%20demoed%20the%20local%20system%20as%20a%20shared%20resource%3A%20the%20Windows%20Forms%20application%20and%20the%20background%20task%20were%20able%20to%20access%20to%20the%20same%20settings%20stored%20in%20the%20file%20system.%20To%20demonstrate%20this%20feature%2C%20we%20have%20expanded%20the%20GitHub%20project%20we%20have%20created%20in%20the%20previous%20post%2C%20which%20you%20can%20download%20from%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fqmatteoq%2FDesktopBridge%2Ftree%2Fmaster%2F5.%2520Extend%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%20https%3A%2F%2Fgithub.com%2Fqmatteoq%2FDesktopBridge%2Ftree%2Fmaster%2F5.%2520Extend%20%3C%2FA%3E%20.%20In%20the%20second%20part%20of%20the%20post%2C%20instead%2C%20we%20demoed%20the%20registry%20as%20a%20shared%20resource%3A%20in%20this%20case%2C%20we%20have%20used%20two%20different%20Windows%20Forms%20applications%20running%20inside%20the%20UWP%20container%20that%20they%20were%20able%20to%20read%20and%20write%20data%20from%20the%20same%20virtualized%20registry.%20You%20can%20download%20the%20sample%20code%20of%20this%20second%20approach%20at%20the%20URL%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fqmatteoq%2FDesktopBridge%2Ftree%2Fmaster%2FExtras%2FContainer%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%20https%3A%2F%2Fgithub.com%2Fqmatteoq%2FDesktopBridge%2Ftree%2Fmaster%2FExtras%2FContainer%3C%2FA%3E%3C%2FP%3E%3CBR%20%2F%3E%3CP%3EHappy%20coding!%3C%2FP%3E%0A%20%0A%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-316581%22%20slang%3D%22en-US%22%3EFirst%20published%20on%20MSDN%20on%20Nov%2028%2C%202016%20In%20the%20previous%20post%20we%20have%20learned%20how%20to%20expand%20a%20desktop%20application%20with%20a%20background%20task%2C%20which%20is%20a%20component%20that%20belongs%20to%20the%20Universal%20Windows%20Platform.%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-316581%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3Edesktop%20bridge%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EUniversal%20Windows%20Platform%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1255898%22%20slang%3D%22en-US%22%3ERe%3A%20Desktop%20Bridge%20%26amp%3B%238211%3B%20Multiple%20desktop%20processes%2C%20a%20single%20UWP%20container%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1255898%22%20slang%3D%22en-US%22%3E%3CP%3EVery%20informative%20article!%3C%2FP%3E%3CP%3EOne%20thing%20that%20is%20still%20unclear%20for%20me%20is%20how%20one%20should%20deal%20with%20possible%20concurrent%20access%20to%20local%20settings%20by%20both%20a%20UWP%20part%20(e.g.%20a%20UI%20or%20a%20background%20task)%20and%20a%20win32%20part%20in%20a%20Desktop%20Bridge%20app.%3C%2FP%3E%3CP%3EI%20have%20an%20app%20where%20a%20win32%20executable%20(packaged%20inside%20the%20app)%20is%20launched%20by%20either%20the%20user%20through%20a%20UI%20or%20a%20periodically%20running%20background%20task.%20The%20UWP%20runtime%20will%20sometimes%20write%20some%20data%20in%20local%20settings%20and%20so%20does%20the%20win32%20executable.%20The%20reason%20for%20letting%20the%20win32%20process%20write%20to%20local%20settings%20instead%20of%20using%20the%20AppService%20connection%20is%20that%20the%20win32%20process%20can%20take%20a%20long%20time%20to%20complete%20(it%20is%20a%20folder%20sync%2Fbackup%20app)%20and%20so%20the%20AppService%20connection%20would%20not%20remain%20active%20when%20it%20is%20launched%20by%20a%20periodic%20background%20task.%3C%2FP%3E%3CP%3EIn%20'normal'%20apps%20we%20protect%20shared%20access%20to%20a%20resource%20with%20a%20semaphore%20but%20that%20as%20far%20as%20I%20know%20would%20not%20help%20here.%3C%2FP%3E%3CP%3EAny%20suggestions%20on%20this%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E
Microsoft
First published on MSDN on Nov 28, 2016

In the previous post we have learned how to expand a desktop application with a background task, which is a component that belongs to the Universal Windows Platform. For the first time, we didn’t have just a single process running inside the UWP container (like we’ve seen in this post , where we were leveraging some UWP APIs but, in the end, the only running process was the Win32 one), but multiple ones: the Windows Forms application and the backgroundTaskHost.exe process, which is the one that Windows uses to handle background tasks.

The purpose of this post is to help you to better understand, with two real world examples, that with the Desktop Bridge, even if we have multiple process inside our AppX, they’re actually running inside the same UWP container. like it’s highlighted in the following image.

The main benefit of this approach is that, even if we’re talking about separate processes, they share the same context and the same resources and they can easily exchange data between them. The simplest and most common example is the local storage: the AppData area, where desktop and UWP application can write and read data, is the same, so the two process can access to the same folders and the same files. Let’s make a real example by expanding the application we have used in the previous post and that you can download from my GitHub repository: https://github.com/qmatteoq/DesktopBridge/tree/master/5.%20Extend

The scenario described in the post was leveraging two different processes:

  • A Win32 one, which is a Windows Forms application that, by leveraging a set of UWP APIs, is registering a background task connected to the TimeZoneTrigger trigger.
  • A UWP one, which is a Windows Runtime Component that takes care of updating the tile of the application and to show a toast notifications.

By combining together the two processes we created a desktop application that, by registering the background task, was able to send a notification to the user and to update its tile every time the user changed the time zone of his computer in the Windows’ settings. In this post we’re going to expand the scenario by adding the following features, which will be made possible by the fact that the two applications are running inside the same container:

  1. In the Windows Forms application, we’re going to add a TextBox control, where the user will have the chance to write a custom message.
  2. By using the UWP APIs to handle settings, we’re going to save this text inside the local storage.
  3. The background task, before running, will try to load the data from the settings and it will include the custom message inside the text displayed in the live tile and in the toast notification sent to the user.

The Windows Forms application

The work to be done in the Windows Forms application is very simple: first, we need to add, above the button we have included in the previous post to register the background task, a TextBox control, where the user will be able to write his custom message. Then, we need to change a little bit the code that is executed when the user presses the button:

private async void OnRegisterTask(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(txtMessage.Text))
{
ApplicationData.Current.LocalSettings.Values.Clear();
ApplicationData.Current.LocalSettings.Values.Add("message", txtMessage.Text);
}

string triggerName = "TimeZoneTriggerTest";

// Check if the task is already registered
foreach (var cur in BackgroundTaskRegistration.AllTasks)
{
if (cur.Value.Name == triggerName)
{
// The task is already registered.
return;
}
}

BackgroundTaskBuilder builder = new BackgroundTaskBuilder();
builder.Name = triggerName;
builder.SetTrigger(new SystemTrigger(SystemTriggerType.TimeZoneChange, false));
builder.TaskEntryPoint = "TileBackgroundTask.TileTask";
var status = await BackgroundExecutionManager.RequestAccessAsync();
if (status != BackgroundAccessStatus.DeniedByUser && status != BackgroundAccessStatus.DeniedBySystemPolicy)
{
builder.Register();
}
}

We can spot the difference compared to the code we have seen in the previous post in the first lines of the event handler: if the Text property of the TextBox control we have added (identified by the name txtMessage ) contains a value, we save it in the local storage using the settings APIs. These APIs are very simple to use: by using the static class ApplicationData.Current.LocalSettings we can access to a collection called Values , which is a dictionary where we can store a set of key / value pairs. In this case, we store an element identified by the key message which contains, as value, the custom message written by the user. The rest of the code is the same we’ve seen in the previous post and it’s the one that takes care of registering the background task in the proper way.


The background task


Inside the background task we’re going to perform the reverse operation: we’re going to check if, in the settings, there’s a value identified by the key called message and, if it’s positive, we read it as a string. Here is the new implementation of the Run() method inside the TileTask of the Windows Runtime Component:

public sealed class TileTask : IBackgroundTask
{
public void Run(IBackgroundTaskInstance taskInstance)
{
string message = string.Empty;
if (ApplicationData.Current.LocalSettings.Values.ContainsKey("message"))
{
message = ApplicationData.Current.LocalSettings.Values["message"].ToString();
}


string tileXml = $@"<tile>
<visual>
<binding template='TileMedium' branding='logo'>
<group>
<subgroup>
<text hint-style='caption'>Time zone changed!</text>
<text hint-style='captionSubtle' hint-wrap='true'>{message}</text>
<text hint-style='captionSubtle' hint-wrap='true'>Last update at {DateTime.Now}</text>
</subgroup>
</group>
</binding>
</visual>
</tile>";

XmlDocument tileDoc = new XmlDocument();
tileDoc.LoadXml(tileXml);

TileNotification notification = new TileNotification(tileDoc);
TileUpdateManager.CreateTileUpdaterForApplication().Update(notification);

string toastXml = $@"<toast>
<visual>
<binding template='ToastGeneric'>
<text>Time zone changed!</text>
<text>{message}</text>
<text>Last update at {DateTime.Now}</text>
</binding>
</visual>
</toast>";

XmlDocument toastDoc = new XmlDocument();
toastDoc.LoadXml(toastXml);

ToastNotification toast = new ToastNotification(toastDoc);
ToastNotificationManager.CreateToastNotifier().Show(toast);
}
}

You can notice that we are using the same APIs that we have used in the desktop application. The only difference is that, before using the Applicationdata.Current.LocalSettings.Values collection to retrieve the value identified by the message key, we use the ContainsKey() method to make sure that such value really exists. If it’s positive, we retrieve it and we store it inside a variable. The rest of the code is the same we’ve seen in the previous post , with just one difference: in the XML payload that describes the tile and the toast notification we have added a new text element, which contains the message retrieved from the settings.


It’s testing time!


Our job is completed: now compile again the solution, launch the UWP deployment project included in your Visual Studio 17 RC’s solution and, this time, before pressing the button to register the task, write a message inside the TextBox. Now repeat the test we made in the previous post: open the Windows’ settings, choose Time & Language –> Date & Time and change the current time zone. You will notice that, this time, the tile and toast notifications will contain, other than the title and the date and time of the last update, also the custom message we have added in the Windows Forms application (in the below image, it’s the text Hello Desktop Bridge! ):




Using the system registry


Let’s see another example of how multiple processes running inside the same UWP container share the same resources. This time, we’re going to use a more traditional approach: no more UWP APIs, but we’re going to use only standard .NET Framework APIs. As we have learned in the first post of the series , applications converted using the Desktop Bridge run inside a container, which we can consider as a virtualized environment: the file system and the registry will be isolated and merged, in real time, with the system ones. This way, the application will continue to have complete access to the registry and work as expected but, when uninstalled, it won’t be able to leave any leftover that, during time, can lead to slow down the operating system. This is made possible by the fact that, when a converted application tries to write some data in the registry, the operation won’t be performed against the system registry, but against a virtualized version (which is simply a special text file). Let’s see a real example to better understand this concept, by creating a new Visual Studio solution: also in this case, to make our life easier, we’re going to use Visual Studio 2017 RC and the UWP deployment extension.

Let’s start by creating a Windows Forms application with a Button control: when it’s pressed, we’re going to use the standard .NET Framework APIs to create a new registry key.

private void OnCreateRegistryKey(Object sender, EventArgs e)
{
var RegKey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Matteo Pagani\DesktopBridge", true);

if (RegKey == null)
{
RegKey = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\Matteo Pagani\DesktopBridge", RegistryKeyPermissionCheck.ReadWriteSubTree);
RegKey.SetValue("IsAppInstalled", true);
}
}

The code uses the Registry class and the OpenSubKey() method to access to a specific registry key inside the hive of the current user (this way, the application will be able to run without requiring admin rights, which is a scenario not supported by converted apps). The key is called IsAppInstalled and it’s stored inside the path HKEY_CURRENT_USER\SOFTWARE\Matteo Pagani\DesktopBridge. If the key doesn’t exist, we create it by leveraging the SetValue() method, passing as parameters the name of the key and its value ( true ).

Let’s start, as a first test, to launch the application as a native Win32 process: let’s click on the button, then open the system registry (by opening the Start menu, writing regedit and pressing Enter). If we did everything correctly, we will find the key we have just created:


Now, from the registry editor, delete the key we have just created (in my case, the entire folder called Matteo Pagani ) and now move on to the next step, which is adding a Desktop to UWP deployment project to our solution. Let’s configure it in a way that we will be able to launch the Windows Forms application we have just created inside the UWP container (I won’t repeat in this post all the required steps, since I’ve already explained this approach in one of the previous posts ), then let’s try to launch it. The operation will complete just fine but, this time, if we repeat the previous steps to look for the HKEY_CURRENT_USER\SOFTWARE\Matteo Pagani\DesktopBridge path in the registry editor, we won’t be able to find it. The reason is that, this time, the application isn’t running as a native process but inside the UWP container and, consequently, it isn’t writing anymore inside the system registry, but inside the virtualized version offered by the container.

There’s an important takeaway that we can learn from this test: when it comes to converted applications, we can’t use the registry as a way to handle the communication between two different application if they aren’t running inside the same UWP container. We can easily see this blocker by adding, to our solution, a new Windows Forms project with another button that, this time, it will read the registry key instead of writing it:

private void OnReadRegistryKey(Object sender, EventArgs e)
{
bool isAppInstalled = false;

var RegKey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Matteo Pagani\DesktopBridge", true);
if (RegKey != null)
{
string result = RegKey.GetValue("IsAppInstalled").ToString();
isAppInstalled = result == "True" ? true : false;
}

string message = isAppInstalled ? "The app is installed" : "The app isn't installed";
MessageBox.Show(message);
}

By using the same APIs we have learned in the previous sample (the Registry.CurrentUser class and the OpenSubKey() method), we check if the HKEY_CURRENT_USER\SOFTWARE\Matteo Pagani\DesktopBridge path exists. If it’s positive, we check if the key identified by the name IsAppInstalled exists and we display a message to the user, based on the outcome of the operation.

If we now try to launch both application as native (so without using the UWP container) and first we create the registry key using the first app, then we read it using the second app, everything will work as expected: we will see the message that the key has been found, since both applications are writing and reading from the system registry. However, if we try to use the UWP deployment project to launch the first app inside the UWP container and the second app, instead, as native, we will get an error message: the key doesn’t exist. The reason is that that the first application, since it runs inside the container, has written the key inside the virtualized registry, while the second application is running as native and, as such, it’s looking for it in the system registry.


How to handle these kind of scenarios? The best answer would be to start using a different approach and start leveraging some features offered by the Universal Windows Platform, like App Services or the new kind of shared storage introduced in the Anniversary Update that allows to read and write files by applications developed by the same publisher. However, we don’t always have the opportunity to refactor our application in the short term, because we may have some business and timing requirements to respect. As such, another approach we can leverage that doesn’t require us to partially rewrite our application is the same we’ve seen at the beginning of this post: when two or more processes are running inside the same container, they share the same resources. In the first example of the post, the resource that we shared was the local storage, but also the registry is one of these resources. As such, if we include all our Win32 processes inside the same AppX package, they will be able to read and write from the same virtualized registry, exactly in the same way in the previous example the Windows Forms application and the background task were able to read and write from the same local storage, since they were both stored in the same AppX package.

The first step to achieve this goal is to change the deployment project so that, when we deploy it, it will include both the Windows Forms applications and not just the original one we have created. Here is how we can configure the AppXPackageFileList.xml file so that it can retrieve, from the Visual Studio output folders, the executables of the two desktop applications and copy them inside the PackageLayout folder:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<MyProjectOutputPath>$(PackageLayout)\..\..\..\Registry</MyProjectOutputPath>
</PropertyGroup>
<ItemGroup>
<LayoutFile Include="$(MyProjectOutputPath)\DesktopApp.WriteRegistry\bin\Debug\DesktopApp.WriteRegistry.exe">
<PackagePath>$(PackageLayout)\DesktopApp.WriteRegistry.exe</PackagePath>
</LayoutFile>
<LayoutFile Include="$(MyProjectOutputPath)\DesktopApp.ReadRegistry\bin\Debug\DesktopApp.ReadRegistry.exe">
<PackagePath>$(PackageLayout)\DesktopApp.ReadRegistry.exe</PackagePath>
</LayoutFile>
</ItemGroup>
</Project>

The approach is similar to the one we’ve seen in one of the previous posts , with the difference that in the previous sample we were including in the same package an executable (a Windows Forms application) and a DLL library (JSON.NET). This time, instead, we include two executable: the two Windows Forms apps we’ve created before (the first one that creates the registry key and the second one that reads the registry key). Since now both applications are part of the same package, they have access to the same resources, including the virtualized version of the registry. Let’s do a concrete example by adding a new button in the first application (the one called DesktopApp.WriteRegisty ) that will invoke the following code:

private void OnOpenReadApp(Object sender, EventArgs e)
{
string result = System.Reflection.Assembly.GetExecutingAssembly().Location;
int index = result.LastIndexOf("\\");
string processPath = $"{result.Substring(0, index)}\\DesktopApp.ReadRegistry.exe";
Process.Start(processPath);
}

The previous code uses the .NET Framework APIs to retrieve, by using the reflection, the path of the running executable, so that it composes the proper path of the other executable which, instead, is called DesktopApp.ReadRegistry.exe . The reason why we used the reflection approach and not the specific API to retrieve the current working directory (the method Directory.GetCurrentDirectory() ) is that, when a desktop application is running inside the UWP container, this API doesn’t return the real current directory, but the Windows’ system one. Consequently, this is one of the possible workarounds we can use to get access to the path of the other executable stored inside the same package. Once we have the full path, we can launch the second application by calling the Process.Start() method.

At this point, the second application will start: this time, if we create first the registry key using the button in the first app and then, in the second app, we press the button to read the same key, the operation will complete with success. The reason is that both applications are running inside the same UWP container and, as such, they are both reading and writing not only inside the same file system (like we’ve seen with the first sample), but also inside the same registry.



Wrapping up


In this post we have learned more about how the UWP container works and how we can share data between two different processes running in the same container. In the first sample, we demoed the local system as a shared resource: the Windows Forms application and the background task were able to access to the same settings stored in the file system. To demonstrate this feature, we have expanded the GitHub project we have created in the previous post, which you can download from https://github.com/qmatteoq/DesktopBridge/tree/master/5.%20Extend . In the second part of the post, instead, we demoed the registry as a shared resource: in this case, we have used two different Windows Forms applications running inside the UWP container that they were able to read and write data from the same virtualized registry. You can download the sample code of this second approach at the URL https://github.com/qmatteoq/DesktopBridge/tree/master/Extras/Container


Happy coding!

1 Comment
Visitor

Very informative article!

One thing that is still unclear for me is how one should deal with possible concurrent access to local settings by both a UWP part (e.g. a UI or a background task) and a win32 part in a Desktop Bridge app.

I have an app where a win32 executable (packaged inside the app) is launched by either the user through a UI or a periodically running background task. The UWP runtime will sometimes write some data in local settings and so does the win32 executable. The reason for letting the win32 process write to local settings instead of using the AppService connection is that the win32 process can take a long time to complete (it is a folder sync/backup app) and so the AppService connection would not remain active when it is launched by a periodic background task.

In 'normal' apps we protect shared access to a resource with a semaphore but that as far as I know would not help here.

Any suggestions on this?