回答編集履歴
2
見直しキャンペーン中
answer
CHANGED
@@ -1,101 +1,100 @@
|
|
1
|
-
ViewModelで`DispatcherTimer
|
2
|
-
|
3
|
-
UIスレッドで動かす意味はないのでこちらなどでもいいでしょう。
|
4
|
-
[Timer クラス (System.Timers) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.timers.timer)
|
5
|
-
[Timer クラス (System.Threading) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.threading.timer)
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
xmlns="http://schemas.microsoft.com/winfx/2006/xaml
|
19
|
-
xmlns:
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
<Label Content="{Binding
|
26
|
-
<Label Content="{Binding
|
27
|
-
<Label Content="{Binding
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
using System;
|
35
|
-
using System.
|
36
|
-
using System.
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
private System.
|
59
|
-
private System.
|
60
|
-
private
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
dispatcherTimer =
|
66
|
-
dispatcherTimer.
|
67
|
-
dispatcherTimer.
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
timersTimer =
|
72
|
-
timersTimer.
|
73
|
-
timersTimer.
|
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
|
-
}
|
1
|
+
ViewModelで`DispatcherTimer`を使ってはダメということはないので、単に移せばいいんじゃないでしょうか。
|
2
|
+
|
3
|
+
UIスレッドで動かす意味はないので、こちらなどでもいいでしょう。
|
4
|
+
[Timer クラス (System.Timers) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.timers.timer)
|
5
|
+
[Timer クラス (System.Threading) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.threading.timer)
|
6
|
+
こういったのもあります。
|
7
|
+
[Observable.Timer Method (System.Reactive.Linq) | Microsoft Docs](https://docs.microsoft.com/en-us/previous-versions/dotnet/reactive-extensions/hh211753(v=vs.103))
|
8
|
+
|
9
|
+
---
|
10
|
+
|
11
|
+
追記 実装例
|
12
|
+
後片付け省略^^;
|
13
|
+
それぞれ同じ意味にしたつもりですが、間違ってたらすいません^^;
|
14
|
+
```xml
|
15
|
+
<Window
|
16
|
+
x:Class="DispatcherTimerTest.MainWindow"
|
17
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
18
|
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
19
|
+
xmlns:local="clr-namespace:DispatcherTimerTest"
|
20
|
+
Width="800"
|
21
|
+
Height="450"
|
22
|
+
DataContext="{x:Static local:ViewModel.Instance}">
|
23
|
+
<StackPanel>
|
24
|
+
<Label Content="{Binding LabelContent1}" />
|
25
|
+
<Label Content="{Binding LabelContent2}" />
|
26
|
+
<Label Content="{Binding LabelContent3}" />
|
27
|
+
<Label Content="{Binding LabelContent4}" />
|
28
|
+
</StackPanel>
|
29
|
+
</Window>
|
30
|
+
```
|
31
|
+
|
32
|
+
```cs
|
33
|
+
using System;
|
34
|
+
using System.ComponentModel;
|
35
|
+
using System.Reactive.Linq;
|
36
|
+
using System.Runtime.CompilerServices;
|
37
|
+
|
38
|
+
namespace DispatcherTimerTest
|
39
|
+
{
|
40
|
+
public class ViewModel : INotifyPropertyChanged
|
41
|
+
{
|
42
|
+
public static ViewModel Instance { get; } = new ViewModel();
|
43
|
+
|
44
|
+
|
45
|
+
public int LabelContent1 { get => _LabelContent1; set => Set(ref _LabelContent1, value); }
|
46
|
+
private int _LabelContent1;
|
47
|
+
|
48
|
+
public int LabelContent2 { get => _LabelContent2; set => Set(ref _LabelContent2, value); }
|
49
|
+
private int _LabelContent2;
|
50
|
+
|
51
|
+
public int LabelContent3 { get => _LabelContent3; set => Set(ref _LabelContent3, value); }
|
52
|
+
private int _LabelContent3;
|
53
|
+
|
54
|
+
public int LabelContent4 { get => _LabelContent4; set => Set(ref _LabelContent4, value); }
|
55
|
+
private int _LabelContent4;
|
56
|
+
|
57
|
+
private System.Windows.Threading.DispatcherTimer dispatcherTimer;
|
58
|
+
private System.Timers.Timer timersTimer;
|
59
|
+
private System.Threading.Timer threadingTimer;
|
60
|
+
private IDisposable observableTimer;
|
61
|
+
|
62
|
+
public ViewModel()
|
63
|
+
{
|
64
|
+
dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
|
65
|
+
dispatcherTimer.Interval = TimeSpan.FromMilliseconds(100);
|
66
|
+
dispatcherTimer.Tick += (s, e) => LabelContent1 += 1;
|
67
|
+
dispatcherTimer.Start();
|
68
|
+
|
69
|
+
|
70
|
+
timersTimer = new System.Timers.Timer(100);
|
71
|
+
timersTimer.Elapsed += (s, e) => LabelContent2 += 1;
|
72
|
+
timersTimer.AutoReset = true;
|
73
|
+
timersTimer.Enabled = true;
|
74
|
+
|
75
|
+
|
76
|
+
threadingTimer = new System.Threading.Timer(x => LabelContent3 += 1, null, TimeSpan.FromMilliseconds(100), TimeSpan.FromMilliseconds(100));
|
77
|
+
|
78
|
+
|
79
|
+
// NuGet System.Reactive.Linq
|
80
|
+
observableTimer = Observable.Timer(TimeSpan.FromMilliseconds(100), TimeSpan.FromMilliseconds(100))
|
81
|
+
.Subscribe(_ => LabelContent4 += 1);
|
82
|
+
|
83
|
+
}
|
84
|
+
|
85
|
+
#region INotifyPropertyChanged
|
86
|
+
public event PropertyChangedEventHandler PropertyChanged;
|
87
|
+
protected bool Set<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
|
88
|
+
{
|
89
|
+
if(Equals(storage, value)) return false;
|
90
|
+
|
91
|
+
storage = value;
|
92
|
+
OnPropertyChanged(propertyName);
|
93
|
+
return true;
|
94
|
+
}
|
95
|
+
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
|
96
|
+
=> PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
97
|
+
#endregion
|
98
|
+
}
|
99
|
+
}
|
101
100
|
```
|
1
追記 実装例
answer
CHANGED
@@ -5,4 +5,97 @@
|
|
5
5
|
[Timer クラス (System.Threading) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.threading.timer)
|
6
6
|
|
7
7
|
こういったのもあります。
|
8
|
-
[Observable.Timer Method (System.Reactive.Linq) | Microsoft Docs](https://docs.microsoft.com/en-us/previous-versions/dotnet/reactive-extensions/hh211753(v=vs.103))
|
8
|
+
[Observable.Timer Method (System.Reactive.Linq) | Microsoft Docs](https://docs.microsoft.com/en-us/previous-versions/dotnet/reactive-extensions/hh211753(v=vs.103))
|
9
|
+
|
10
|
+
---
|
11
|
+
|
12
|
+
追記 実装例
|
13
|
+
後片付け省略^^;
|
14
|
+
それぞれ同じ意味にしたつもりですが、間違ってたらすいません^^;
|
15
|
+
```xaml
|
16
|
+
<Window
|
17
|
+
x:Class="DispatcherTimerTest.MainWindow"
|
18
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
19
|
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
20
|
+
xmlns:local="clr-namespace:DispatcherTimerTest"
|
21
|
+
Width="800"
|
22
|
+
Height="450"
|
23
|
+
DataContext="{x:Static local:ViewModel.Instance}">
|
24
|
+
<StackPanel>
|
25
|
+
<Label Content="{Binding LabelContent1}" />
|
26
|
+
<Label Content="{Binding LabelContent2}" />
|
27
|
+
<Label Content="{Binding LabelContent3}" />
|
28
|
+
<Label Content="{Binding LabelContent4}" />
|
29
|
+
</StackPanel>
|
30
|
+
</Window>
|
31
|
+
```
|
32
|
+
|
33
|
+
```C#
|
34
|
+
using System;
|
35
|
+
using System.ComponentModel;
|
36
|
+
using System.Reactive.Linq;
|
37
|
+
using System.Runtime.CompilerServices;
|
38
|
+
|
39
|
+
namespace DispatcherTimerTest
|
40
|
+
{
|
41
|
+
public class ViewModel : INotifyPropertyChanged
|
42
|
+
{
|
43
|
+
public static ViewModel Instance { get; } = new ViewModel();
|
44
|
+
|
45
|
+
|
46
|
+
public int LabelContent1 { get => _LabelContent1; set => Set(ref _LabelContent1, value); }
|
47
|
+
private int _LabelContent1;
|
48
|
+
|
49
|
+
public int LabelContent2 { get => _LabelContent2; set => Set(ref _LabelContent2, value); }
|
50
|
+
private int _LabelContent2;
|
51
|
+
|
52
|
+
public int LabelContent3 { get => _LabelContent3; set => Set(ref _LabelContent3, value); }
|
53
|
+
private int _LabelContent3;
|
54
|
+
|
55
|
+
public int LabelContent4 { get => _LabelContent4; set => Set(ref _LabelContent4, value); }
|
56
|
+
private int _LabelContent4;
|
57
|
+
|
58
|
+
private System.Windows.Threading.DispatcherTimer dispatcherTimer;
|
59
|
+
private System.Timers.Timer timersTimer;
|
60
|
+
private System.Threading.Timer threadingTimer;
|
61
|
+
private IDisposable observableTimer;
|
62
|
+
|
63
|
+
public ViewModel()
|
64
|
+
{
|
65
|
+
dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
|
66
|
+
dispatcherTimer.Interval = TimeSpan.FromMilliseconds(100);
|
67
|
+
dispatcherTimer.Tick += (s, e) => LabelContent1 += 1;
|
68
|
+
dispatcherTimer.Start();
|
69
|
+
|
70
|
+
|
71
|
+
timersTimer = new System.Timers.Timer(100);
|
72
|
+
timersTimer.Elapsed += (s, e) => LabelContent2 += 1;
|
73
|
+
timersTimer.AutoReset = true;
|
74
|
+
timersTimer.Enabled = true;
|
75
|
+
|
76
|
+
|
77
|
+
threadingTimer = new System.Threading.Timer(x => LabelContent3 += 1, null, TimeSpan.FromMilliseconds(100), TimeSpan.FromMilliseconds(100));
|
78
|
+
|
79
|
+
|
80
|
+
// NuGet System.Reactive.Linq
|
81
|
+
observableTimer = Observable.Timer(TimeSpan.FromMilliseconds(100), TimeSpan.FromMilliseconds(100))
|
82
|
+
.Subscribe(_ => LabelContent4 += 1);
|
83
|
+
|
84
|
+
}
|
85
|
+
|
86
|
+
#region INotifyPropertyChanged
|
87
|
+
public event PropertyChangedEventHandler PropertyChanged;
|
88
|
+
protected bool Set<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
|
89
|
+
{
|
90
|
+
if(Equals(storage, value)) return false;
|
91
|
+
|
92
|
+
storage = value;
|
93
|
+
OnPropertyChanged(propertyName);
|
94
|
+
return true;
|
95
|
+
}
|
96
|
+
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
|
97
|
+
=> PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
98
|
+
#endregion
|
99
|
+
}
|
100
|
+
}
|
101
|
+
```
|