Uwp thread to task fixes (#8639)
authorShane Neuville <shneuvil@microsoft.com>
Wed, 4 Dec 2019 17:39:53 +0000 (10:39 -0700)
committerGitHub <noreply@github.com>
Wed, 4 Dec 2019 17:39:53 +0000 (10:39 -0700)
* Convert usages of thread in gallery to Task

* - swap thread for task

* - couple more thread to task fixes

* - remove call to start

Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla41418.cs
Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/GitHub1702.cs
Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue1704.cs
Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue1724.cs
Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue3367.cs
Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue5376.cs
Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue6282.xaml.cs
Xamarin.Forms.Controls/GalleryPages/CollectionViewGalleries/EmptyViewGalleries/EmptyViewLoadSimulateGallery.xaml.cs

index f505eaa..35db9d9 100644 (file)
@@ -2,6 +2,7 @@
 using System.Threading;
 using Xamarin.Forms.CustomAttributes;
 using Xamarin.Forms.Internals;
+using System.Threading.Tasks;
 
 #if UITEST
 using Xamarin.UITest;
@@ -46,12 +47,12 @@ namespace Xamarin.Forms.Controls.Issues
                        var disappeared = false;
 
                        // change margin of box after the first rendering
-                       new Thread(() => {
+                       Task.Run(async () => {
                                while (true)
                                {
                                        for (int margin = 20; margin < 160; margin += 20)
                                        {
-                                               Thread.Sleep(1000);
+                                               await Task.Delay(1000);
                                                if (disappeared)
                                                        return;
                                                Device.BeginInvokeOnMainThread(() =>
index 992996f..35ae95b 100644 (file)
@@ -4,6 +4,7 @@ using System;
 using System.Linq;
 using System.Threading;
 using System.Collections.Generic;
+using System.Threading.Tasks;
 
 namespace Xamarin.Forms.Controls.Issues
 {
@@ -59,7 +60,7 @@ namespace Xamarin.Forms.Controls.Issues
 
                        var buttons = layout.Children.OfType<Button>();
                        layout.Children.Insert(0, ActionGrid(buttons.ToList()));
-                       PaddingAnimation(buttons).Start();
+                       PaddingAnimation(buttons);
 
                        Content = layout;
                }
@@ -122,9 +123,9 @@ namespace Xamarin.Forms.Controls.Issues
                        return actionGrid;
                }
 
-               Thread PaddingAnimation(IEnumerable<Button> buttons)
+               void PaddingAnimation(IEnumerable<Button> buttons)
                {
-                       return new Thread(() =>
+                       Task.Run(async () =>
                        {
                                int increment = 1;
                                int current = 0;
@@ -134,7 +135,7 @@ namespace Xamarin.Forms.Controls.Issues
 
                                while (true)
                                {
-                                       Thread.Sleep(sleep);
+                                       await Task.Delay(sleep);
 
                                        if (!animation)
                                                continue;
index 1160456..815836e 100644 (file)
@@ -3,6 +3,7 @@ using System.Collections.Generic;
 using System.Threading;
 using Xamarin.Forms.Internals;
 using Xamarin.Forms.CustomAttributes;
+using System.Threading.Tasks;
 
 #if UITEST
 using Xamarin.UITest;
@@ -459,9 +460,9 @@ namespace Xamarin.Forms.Controls.Issues
                                _abortStressTest = false;
 
                                int.TryParse(_stressTestItertionEntry.Text, out _stressTestIterationCount);
-        
+
 #if __UWP__
-                               Windows.System.Threading.ThreadPool.RunAsync(delegate { runStressTest(); });
+                               Task.Run(runStressTest);
 #else
                                ThreadPool.QueueUserWorkItem(delegate { runStressTest(); });
 #endif
@@ -499,7 +500,7 @@ namespace Xamarin.Forms.Controls.Issues
                        };
                }
 
-               void runStressTest()
+               async void runStressTest()
                {
                        for (int i = 0; i < _stressTestIterationCount && !_abortStressTest; i++)
                        {
@@ -521,9 +522,9 @@ namespace Xamarin.Forms.Controls.Issues
                                _nextStressTest.WaitOne();
 
                                while (_stressTestImage.IsLoading)
-                                       Thread.Sleep(10);
+                                       await Task.Delay(10).ConfigureAwait(false);
 
-                               Thread.Sleep(10);
+                               await Task.Delay(10).ConfigureAwait(false);
                        }
 
                        Device.BeginInvokeOnMainThread(() => {
index 298e7d0..a4a0b7e 100644 (file)
@@ -6,6 +6,7 @@ using System.Threading;
 using System.Collections.Generic;
 using Xamarin.Forms.PlatformConfiguration;
 using Xamarin.Forms.PlatformConfiguration.AndroidSpecific;
+using System.Threading.Tasks;
 
 namespace Xamarin.Forms.Controls.Issues
 {
@@ -164,7 +165,7 @@ namespace Xamarin.Forms.Controls.Issues
 
                        var buttons = layout.Children.OfType<ImageButton>();
                        layout.Children.Insert(0, ActionGrid(buttons.ToList()));
-                       PaddingAnimation(buttons).Start();
+                       PaddingAnimation(buttons);
 
                        Content = new ScrollView() { Content = layout };
                }
@@ -218,9 +219,9 @@ namespace Xamarin.Forms.Controls.Issues
                        return actionGrid;
                }
 
-               Thread PaddingAnimation(IEnumerable<ImageButton> buttons)
+               void PaddingAnimation(IEnumerable<ImageButton> buttons)
                {
-                       return new Thread(() =>
+                       Task.Run(async () =>
                        {
                                int increment = 1;
                                int current = 0;
@@ -230,7 +231,7 @@ namespace Xamarin.Forms.Controls.Issues
 
                                while (true)
                                {
-                                       Thread.Sleep(sleep);
+                                       await Task.Delay(sleep);
 
                                        if (!animation)
                                                continue;
index af9d98f..5b9339d 100644 (file)
@@ -2,6 +2,7 @@
 using Xamarin.Forms.Internals;
 using System.Collections.ObjectModel;
 using System.Threading;
+using System.Threading.Tasks;
 
 namespace Xamarin.Forms.Controls.Issues
 {
@@ -22,11 +23,11 @@ namespace Xamarin.Forms.Controls.Issues
                        var lstMessages = new ListView();
                        lstMessages.SetBinding(ListView.ItemsSourceProperty, ".");
 
-                       new Thread(() =>
+                       Task.Run(async () =>
                        {
                                while (simulatedThreadEnabled)
                                {
-                                       Thread.Sleep(500);
+                                       await Task.Delay(500);
                                        if (!simulateMessages)
                                                continue;
 
@@ -34,7 +35,7 @@ namespace Xamarin.Forms.Controls.Issues
                                        collection.Add(newItem);
                                        Device.BeginInvokeOnMainThread(() => lstMessages.ScrollTo(newItem, ScrollToPosition.Start, false));
                                }
-                       }).Start();
+                       });
                        
                        this.Disappearing += (_, __) => simulatedThreadEnabled = false;
 
index bb71723..e7a70e7 100644 (file)
@@ -62,11 +62,11 @@ namespace Xamarin.Forms.Controls.Issues
                                base.OnAppearing();
 
                                entry.Focus();
-                               new Thread(() =>
+                               Task.Run(async () =>
                                {
                                        while (!IsTested)
                                        {
-                                               Thread.Sleep(100);
+                                               await Task.Delay(100);
                                                Device.BeginInvokeOnMainThread(() =>
                                                {
                                                        if (entry.IsFocused)
@@ -80,7 +80,7 @@ namespace Xamarin.Forms.Controls.Issues
                                                        }
                                                });
                                        }
-                               }).Start();
+                               });
                        }
                }
 
@@ -92,4 +92,4 @@ namespace Xamarin.Forms.Controls.Issues
                }
 #endif
        }
-}
\ No newline at end of file
+}
index 638ace0..5e46b6d 100644 (file)
@@ -1,4 +1,5 @@
 using System.Threading;
+using System.Threading.Tasks;
 using Xamarin.Forms.CustomAttributes;
 using Xamarin.Forms.Internals;
 
@@ -12,15 +13,15 @@ namespace Xamarin.Forms.Controls.Issues
                {
 #if APP
                        InitializeComponent();
-                       new Thread(() =>
+                       Task.Run(async () =>
                        {
-                               Thread.Sleep(1000);
+                               await Task.Delay(1000);
                                SwitchIsEnabled();
-                               Thread.Sleep(1500);
+                               await Task.Delay(1500);
                                SwitchIsEnabled();
-                               Thread.Sleep(1000);
+                               await Task.Delay(1000);
                                SwitchIsEnabled();
-                       }).Start();
+                       });
 #endif
                }
 
index 839cc8c..a54aa51 100644 (file)
@@ -1,5 +1,6 @@
 using System.Collections.Generic;
 using System.Threading;
+using System.Threading.Tasks;
 using Xamarin.Forms.Xaml;
 
 namespace Xamarin.Forms.Controls.GalleryPages.CollectionViewGalleries.EmptyViewGalleries
@@ -15,13 +16,13 @@ namespace Xamarin.Forms.Controls.GalleryPages.CollectionViewGalleries.EmptyViewG
 
                        CollectionView.ItemTemplate = ExampleTemplates.PhotoTemplate();
 
-                       new Thread(() =>
+                       Task.Run(async () =>
                        {
-                               Thread.Sleep(1000);
+                               await Task.Delay(1000);
                                Device.BeginInvokeOnMainThread(() => CollectionView.ItemsSource = new List<object>());
-                               Thread.Sleep(1000);
+                               await Task.Delay(1000);
                                Device.BeginInvokeOnMainThread(() => CollectionView.ItemsSource = _demoFilteredItemSource.Items);
-                       }).Start();
+                       });
                }
        }
 }
\ No newline at end of file