summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla28953.cs
blob: 3a35786325f98c63413af5040681e79bf8b6ec7d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;
using System;
using System.Threading.Tasks;

#if UITEST
using Xamarin.UITest;
using NUnit.Framework;
#endif

namespace Xamarin.Forms.Controls.Issues
{
	[Preserve(AllMembers = true)]
	[Issue(IssueTracker.Bugzilla, 28953, "Device.StartTimer (still) behaves differently on different platforms", PlatformAffected.All)]
	public class Bugzilla28953 : TestContentPage // or TestMasterDetailPage, etc ...
	{
		int count = 0, count2 = 0;
		Label label2, label3;
		bool shouldStop, shouldStop2;

		protected override void Init()
		{
			var stackLayout = new StackLayout
			{
				Orientation = StackOrientation.Vertical,
				VerticalOptions = LayoutOptions.Center,
				Spacing = 20
			};

			var label1 = new Label
			{
				Text = "Click Start to start counting with a timer. Click Stop to reset. Both timers update text in UI thread."
			};
			stackLayout.Children.Add(label1);

			label2 = new Label
			{
				Text = count.ToString(),
				HorizontalTextAlignment = TextAlignment.Center,
			};
			stackLayout.Children.Add(label2);

			label3 = new Label
			{
				Text = count2.ToString(),
				HorizontalTextAlignment = TextAlignment.Center,
			};
			stackLayout.Children.Add(label3);

			var button = new Button
			{
				Text = "Start"
			};
			button.Clicked += Button_Clicked;
			stackLayout.Children.Add(button);

			var button2 = new Button
			{
				Text = "Start (in background thread)"
			};
			button2.Clicked += Button_Clicked2;
			stackLayout.Children.Add(button2);

			Content = stackLayout;
		}

		private void Button_Clicked(object sender, EventArgs e)
		{
			var button = sender as Button;
			if (button.Text == "Start")
			{
				(sender as Button).Text = "Stop";
				shouldStop = false;

				Device.StartTimer(TimeSpan.FromMilliseconds(100), () =>
				{
					label2.Text = count.ToString();
					count++;
					return !shouldStop;
				});
			}
			else
			{
				button.Text = "Start";
				shouldStop = true;
				count = 0;
			}
		}

		private void Button_Clicked2(object sender, EventArgs e)
		{
			var button = sender as Button;
			if (button.Text == "Start (in background thread)")
			{
				(sender as Button).Text = "Stop (in background thread)";
				shouldStop2 = false;

				Task.Run(() =>
				{
					Device.StartTimer(TimeSpan.FromMilliseconds(100), () =>
					{
						label3.Text = count2.ToString();
						count2++;
						return !shouldStop2;
					});
				});
			}
			else
			{
				button.Text = "Start (in background thread)";
				shouldStop2 = true;
				count2 = 0;
			}
		}
	}
}