Browse Source

Correctly verified access from UI thread

Used Dispatcher.UIThread.VerifyAccess() instead of a
custom implementation
sdoroff 8 years ago
parent
commit
8ce4be6294

+ 3 - 11
src/Avalonia.Controls/Calendar/CalendarBlackoutDatesCollection.cs

@@ -3,6 +3,7 @@
 // Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
 // All other rights reserved.
 
+using Avalonia.Threading;
 using System;
 using System.Collections.ObjectModel;
 using System.Linq;
@@ -16,12 +17,7 @@ namespace Avalonia.Controls.Primitives
         /// The Calendar whose dates this object represents.
         /// </summary>
         private Calendar _owner;
-
-        /// <summary>
-        /// The dispatcher thread.
-        /// </summary>
-        private Thread _dispatcherThread;
-
+        
         /// <summary>
         /// Initializes a new instance of the
         /// <see cref="T:Avalonia.Controls.Primitives.CalendarBlackoutDatesCollection" />
@@ -34,7 +30,6 @@ namespace Avalonia.Controls.Primitives
         public CalendarBlackoutDatesCollection(Calendar owner)
         {
             _owner = owner ?? throw new ArgumentNullException(nameof(owner));
-            _dispatcherThread = Thread.CurrentThread;
         }
 
         /// <summary>
@@ -214,10 +209,7 @@ namespace Avalonia.Controls.Primitives
         
         private void EnsureValidThread()
         {
-            if (Thread.CurrentThread != _dispatcherThread)
-            {
-                throw new NotSupportedException("This type of Collection does not support changes to its SourceCollection from a thread different from the Dispatcher thread.");
-            }
+            Dispatcher.UIThread.VerifyAccess();
         }
     }
 }

+ 9 - 25
src/Avalonia.Controls/Calendar/SelectedDatesCollection.cs

@@ -3,6 +3,7 @@
 // Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
 // All other rights reserved.
 
+using Avalonia.Threading;
 using System;
 using System.Collections.ObjectModel;
 using System.Threading;
@@ -15,12 +16,7 @@ namespace Avalonia.Controls.Primitives
         /// Inherited code: Requires comment.
         /// </summary>
         private Collection<DateTime> _addedItems;
-
-        /// <summary>
-        /// Inherited code: Requires comment.
-        /// </summary>
-        private Thread _dispatcherThread;
-
+        
         /// <summary>
         /// Inherited code: Requires comment.
         /// </summary>
@@ -49,7 +45,6 @@ namespace Avalonia.Controls.Primitives
         {
             _owner = owner;
             _addedItems = new Collection<DateTime>();
-            _dispatcherThread = Thread.CurrentThread;
         }
 
         private void InvokeCollectionChanged(System.Collections.IList removedItems, System.Collections.IList addedItems)
@@ -138,10 +133,8 @@ namespace Avalonia.Controls.Primitives
         /// </remarks>
         protected override void ClearItems()
         {
-            if (!IsValidThread())
-            {
-                throw new NotSupportedException("This type of Collection does not support changes to its SourceCollection from a thread different from the Dispatcher thread.");
-            }
+            EnsureValidThread();
+
             Collection<DateTime> addedItems = new Collection<DateTime>();
             Collection<DateTime> removedItems = new Collection<DateTime>();
 
@@ -177,10 +170,7 @@ namespace Avalonia.Controls.Primitives
         /// </remarks>
         protected override void InsertItem(int index, DateTime item)
         {
-            if (!IsValidThread())
-            {
-                throw new NotSupportedException("This type of Collection does not support changes to its SourceCollection from a thread different from the Dispatcher thread.");
-            }
+            EnsureValidThread();
 
             if (!Contains(item))
             {
@@ -243,10 +233,7 @@ namespace Avalonia.Controls.Primitives
         /// </remarks>
         protected override void RemoveItem(int index)
         {
-            if (!IsValidThread())
-            {
-                throw new NotSupportedException("This type of Collection does not support changes to its SourceCollection from a thread different from the Dispatcher thread.");
-            }
+            EnsureValidThread();
 
             if (index >= Count)
             {
@@ -297,10 +284,7 @@ namespace Avalonia.Controls.Primitives
         /// </remarks>
         protected override void SetItem(int index, DateTime item)
         {
-            if (!IsValidThread())
-            {
-                throw new NotSupportedException("This type of Collection does not support changes to its SourceCollection from a thread different from the Dispatcher thread.");
-            }
+            EnsureValidThread();
 
             if (!Contains(item))
             {
@@ -369,9 +353,9 @@ namespace Avalonia.Controls.Primitives
             return true;
         }
         
-        private bool IsValidThread()
+        private void EnsureValidThread()
         {
-            return Thread.CurrentThread == _dispatcherThread;
+            Dispatcher.UIThread.VerifyAccess();
         }
     }
 }