1111using  System . Runtime . CompilerServices ; 
1212using  System . Threading ; 
1313using  System . Threading . Tasks ; 
14- using  Windows . ApplicationModel . Core ; 
14+ using  Microsoft . Toolkit . Uwp . Helpers ; 
1515using  Windows . Devices . Bluetooth ; 
1616using  Windows . Devices . Bluetooth . GenericAttributeProfile ; 
1717using  Windows . Devices . Enumeration ; 
18+ using  Windows . System ; 
1819using  Windows . UI . Core ; 
1920using  Windows . UI . Xaml . Media . Imaging ; 
2021
@@ -134,17 +135,22 @@ public int Compare(object x, object y)
134135        private  ObservableCollection < ObservableGattDeviceService >  _services  = 
135136            new  ObservableCollection < ObservableGattDeviceService > ( ) ; 
136137
138+         private  DispatcherQueue  _dispatcherQueue ; 
139+ 
137140        /// <summary> 
138141        /// Initializes a new instance of the <see cref="ObservableBluetoothLEDevice"/> class. 
139142        /// </summary> 
140143        /// <param name="deviceInfo">The device information.</param> 
141-         public  ObservableBluetoothLEDevice ( DeviceInformation  deviceInfo ) 
144+         /// <param name="dispatcherQueue">The DispatcherQueue that should be used to dispatch UI updates for this BluetoothLE Device, or null if this is being called from the UI thread.</param> 
145+         public  ObservableBluetoothLEDevice ( DeviceInformation  deviceInfo ,  DispatcherQueue  dispatcherQueue  =  null ) 
142146        { 
143147            DeviceInfo  =  deviceInfo ; 
144148            Name  =  DeviceInfo . Name ; 
145149
146150            IsPaired  =  DeviceInfo . Pairing . IsPaired ; 
147151
152+             _dispatcherQueue  =  dispatcherQueue  ??  DispatcherQueue . GetForCurrentThread ( ) ; 
153+ 
148154            LoadGlyph ( ) ; 
149155
150156            this . PropertyChanged  +=  ObservableBluetoothLEDevice_PropertyChanged ; 
@@ -395,7 +401,8 @@ private void ObservableBluetoothLEDevice_PropertyChanged(object sender, Property
395401        /// <exception cref="Exception">Thorws Exception when no permission to access device</exception> 
396402        public  async  Task  ConnectAsync ( ) 
397403        { 
398-             await  CoreApplication . MainView . CoreWindow . Dispatcher . RunAsync ( CoreDispatcherPriority . Normal ,  async  ( )  => 
404+             await  _dispatcherQueue . ExecuteOnUIThreadAsync ( 
405+                 async  ( )  => 
399406            { 
400407                if  ( BluetoothLEDevice  ==  null ) 
401408                { 
@@ -442,7 +449,7 @@ await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPrio
442449                        throw  new  Exception ( _result . ProtocolError . GetErrorString ( ) ) ; 
443450                    } 
444451                } 
445-             } ) ; 
452+             } ,   DispatcherQueuePriority . Normal ) ; 
446453        } 
447454
448455        /// <summary> 
@@ -468,8 +475,7 @@ public async Task DoInAppPairingAsync()
468475        /// <returns>The task of the update.</returns> 
469476        public  async  Task  UpdateAsync ( DeviceInformationUpdate  deviceUpdate ) 
470477        { 
471-             await  CoreApplication . MainView . CoreWindow . Dispatcher . RunAsync ( 
472-                 CoreDispatcherPriority . Normal , 
478+             await  _dispatcherQueue . ExecuteOnUIThreadAsync ( 
473479                ( )  => 
474480                { 
475481                    DeviceInfo . Update ( deviceUpdate ) ; 
@@ -479,7 +485,7 @@ await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(
479485
480486                    LoadGlyph ( ) ; 
481487                    OnPropertyChanged ( "DeviceInfo" ) ; 
482-                 } ) ; 
488+                 } ,   DispatcherQueuePriority . Normal ) ; 
483489        } 
484490
485491        /// <summary> 
@@ -512,9 +518,7 @@ protected virtual void OnPropertyChanged([CallerMemberName] string propertyName
512518        /// <param name="args">The arguments.</param> 
513519        private  async  void  BluetoothLEDevice_NameChanged ( BluetoothLEDevice  sender ,  object  args ) 
514520        { 
515-             await  CoreApplication . MainView . CoreWindow . Dispatcher . RunAsync ( 
516-                 CoreDispatcherPriority . Normal , 
517-                 ( )  =>  {  Name  =  BluetoothLEDevice . Name ;  } ) ; 
521+             await  _dispatcherQueue . ExecuteOnUIThreadAsync ( ( )  =>  {  Name  =  BluetoothLEDevice . Name ;  } ,  DispatcherQueuePriority . Normal ) ; 
518522        } 
519523
520524        /// <summary> 
@@ -524,29 +528,27 @@ await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(
524528        /// <param name="args">The arguments.</param> 
525529        private  async  void  BluetoothLEDevice_ConnectionStatusChanged ( BluetoothLEDevice  sender ,  object  args ) 
526530        { 
527-             await  CoreApplication . MainView . CoreWindow . Dispatcher . RunAsync ( 
528-                 CoreDispatcherPriority . Normal , 
531+             await  _dispatcherQueue . ExecuteOnUIThreadAsync ( 
529532                ( )  => 
530533                { 
531534                    IsPaired  =  DeviceInfo . Pairing . IsPaired ; 
532535                    IsConnected  =  BluetoothLEDevice . ConnectionStatus  ==  BluetoothConnectionStatus . Connected ; 
533-                 } ) ; 
536+                 } ,   DispatcherQueuePriority . Normal ) ; 
534537        } 
535538
536539        /// <summary> 
537540        /// Load the glyph for this device 
538541        /// </summary> 
539542        private  async  void  LoadGlyph ( ) 
540543        { 
541-             await  CoreApplication . MainView . CoreWindow . Dispatcher . RunAsync ( 
542-                 CoreDispatcherPriority . Normal , 
544+             await  _dispatcherQueue . ExecuteOnUIThreadAsync ( 
543545                async  ( )  => 
544546                { 
545547                    var  deviceThumbnail  =  await  DeviceInfo . GetGlyphThumbnailAsync ( ) ; 
546548                    var  glyphBitmapImage  =  new  BitmapImage ( ) ; 
547549                    await  glyphBitmapImage . SetSourceAsync ( deviceThumbnail ) ; 
548550                    Glyph  =  glyphBitmapImage ; 
549-                 } ) ; 
551+                 } ,   DispatcherQueuePriority . Normal ) ; 
550552        } 
551553    } 
552554} 
0 commit comments