Skip to content

Commit c4f2444

Browse files
Merge pull request #1152 from beto-rodriguez/dev
2.0.0-beta.910
2 parents 5329be3 + eac8c68 commit c4f2444

File tree

29 files changed

+67
-142
lines changed

29 files changed

+67
-142
lines changed

samples/AvaloniaSample/Bars/Race/View.axaml.cs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,6 @@ public class View : UserControl
1111
public View()
1212
{
1313
InitializeComponent();
14-
Update();
15-
}
16-
17-
public async void Update()
18-
{
19-
var vm = (ViewModel?)DataContext;
20-
if (vm is null) return;
21-
while (true)
22-
{
23-
Dispatcher.UIThread.Post(vm.RandomIncrement, DispatcherPriority.Background);
24-
await Task.Delay(100);
25-
}
2614
}
2715

2816
private void InitializeComponent()

samples/BlazorSample/Pages/Bars/Race.razor

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,7 @@
1313
protected override void OnAfterRender(bool firstRender)
1414
{
1515
base.OnAfterRender(firstRender);
16-
17-
Update();
1816
}
1917

2018
public ViewModel ViewModel { get; set; } = new();
21-
22-
public async void Update()
23-
{
24-
while (true)
25-
{
26-
ViewModel.RandomIncrement();
27-
await Task.Delay(100);
28-
}
29-
}
3019
}

samples/EtoFormsSample/Bars/Race/View.cs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,6 @@ public View()
2424

2525
Content = cartesianChart;
2626

27-
UpdateViewModel();
28-
2927
UnLoad += (o, e) => Visible = false;
3028
}
31-
32-
public async void UpdateViewModel()
33-
{
34-
while (Visible)
35-
{
36-
viewModel.RandomIncrement();
37-
cartesianChart.Series = viewModel.Series;
38-
await Task.Delay(100);
39-
}
40-
}
4129
}

samples/MauiSample/Bars/Race/View.xaml.cs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,5 @@ public partial class View : ContentPage
88
public View()
99
{
1010
InitializeComponent();
11-
Update();
12-
}
13-
14-
public async void Update()
15-
{
16-
var vm = (ViewModel)BindingContext;
17-
while (true)
18-
{
19-
_ = Dispatcher.Dispatch(vm.RandomIncrement);
20-
await Task.Delay(100);
21-
}
2211
}
2312
}

samples/UnoPlatformSample/UnoPlatformSample/LiveChartsSamples/Bars/Race/View.xaml.cs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,5 @@ public sealed partial class View : UserControl
99
public View()
1010
{
1111
InitializeComponent();
12-
Update();
13-
}
14-
15-
public async void Update()
16-
{
17-
var vm = (ViewModel)DataContext;
18-
while (true)
19-
{
20-
_ = Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => vm.RandomIncrement());
21-
await Task.Delay(100);
22-
}
2312
}
2413
}

samples/ViewModelsSamples/Bars/Race/ViewModel.cs

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-

2-
using System;
1+
using System;
32
using System.Linq;
3+
using System.Threading.Tasks;
44
using CommunityToolkit.Mvvm.ComponentModel;
55
using LiveChartsCore;
66
using LiveChartsCore.Defaults;
@@ -26,6 +26,13 @@ private static readonly (string, double)[] s_initialData =
2626
("Hamilton", 1000)
2727
};
2828

29+
public ViewModel()
30+
{
31+
_ = StartRace();
32+
}
33+
34+
public bool IsReading { get; set; } = true;
35+
2936
[ObservableProperty]
3037
private ISeries[] _series =
3138
s_initialData
@@ -49,16 +56,28 @@ private static readonly (string, double)[] s_initialData =
4956
[ObservableProperty]
5057
private Axis[] _yAxes = { new Axis { IsVisible = false } };
5158

52-
public void RandomIncrement()
59+
public async Task StartRace()
5360
{
54-
foreach (var item in Series)
61+
await Task.Delay(1000);
62+
63+
// to keep this sample simple, we run the next infinite loop
64+
// in a real application you should stop the loop/task when the view is disposed
65+
66+
while (IsReading)
5567
{
56-
if (item.Values is null) continue;
68+
foreach (var item in Series)
69+
{
70+
if (item.Values is null) continue;
5771

58-
var i = ((ObservableValue[])item.Values)[0];
59-
i.Value += _r.Next(0, 100);
60-
}
72+
var i = ((ObservableValue[])item.Values)[0];
73+
i.Value += _r.Next(0, 100);
74+
}
6175

62-
Series = Series.OrderByDescending(x => ((ObservableValue[])x.Values!)[0].Value).ToArray();
76+
Series = Series
77+
.OrderByDescending(x => ((ObservableValue[])x.Values!)[0].Value)
78+
.ToArray();
79+
80+
await Task.Delay(100);
81+
}
6382
}
6483
}

samples/ViewModelsSamples/General/MultiThreading/ViewModel.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,16 @@ public ViewModel()
5555

5656
public object Sync { get; } = new object();
5757

58-
private async void ReadData()
58+
public bool IsReading { get; set; } = true;
59+
60+
private async Task ReadData()
5961
{
6062
await Task.Delay(1000);
6163

62-
while (true)
64+
// to keep this sample simple, we run the next infinite loop
65+
// in a real application you should stop the loop/task when the view is disposed
66+
67+
while (IsReading)
6368
{
6469
await Task.Delay(_delay);
6570

samples/ViewModelsSamples/General/MultiThreading2/ViewModel.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,22 @@ public ViewModel(Action<Action> uiThreadInvoker)
4848
// create {readTasks} parallel tasks that will add a point every {_delay} milliseconds
4949
for (var i = 0; i < readTasks; i++)
5050
{
51-
ReadData();
51+
_ = Task.Run(ReadData);
5252
}
5353
}
5454

5555
public ISeries[] Series { get; set; }
5656

57-
public async void ReadData()
57+
public bool IsReading { get; set; } = true;
58+
59+
public async Task ReadData()
5860
{
5961
await Task.Delay(1000);
6062

61-
while (true)
63+
// to keep this sample simple, we run the next infinite loop
64+
// in a real application you should stop the loop/task when the view is disposed
65+
66+
while (IsReading)
6267
{
6368
await Task.Delay(_delay);
6469

samples/ViewModelsSamples/Pies/Custom/ViewModel.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ public partial class ViewModel : ObservableObject
1313
public ViewModel()
1414
{
1515
var outer = 1d;
16-
//var data = new[] { 6, 5, 4, 3 };
17-
var data = new[] { 6 };
16+
var data = new[] { 6, 5, 4, 3 };
1817

1918
// you can convert any array, list or IEnumerable<T> to a pie series collection:
2019
Series = data.AsPieSeries((value, series) =>

samples/WPFSample/Bars/Race/View.xaml.cs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,5 @@ public partial class View : UserControl
1313
public View()
1414
{
1515
InitializeComponent();
16-
Update();
17-
}
18-
19-
public async void Update()
20-
{
21-
var vm = (ViewModel)DataContext;
22-
while (true)
23-
{
24-
Application.Current.Dispatcher.Invoke(vm.RandomIncrement);
25-
await Task.Delay(100);
26-
}
2716
}
2817
}

0 commit comments

Comments
 (0)