回答編集履歴

2

見直しキャンペーン中

2023/07/22 08:13

投稿

TN8001
TN8001

スコア9326

test CHANGED
@@ -1,201 +1,100 @@
1
- ViewModelで`DispatcherTimer `を使ってはダメということはないので、単に移せばいいんじゃないでしょうか。
1
+ ViewModelで`DispatcherTimer`を使ってはダメということはないので、単に移せばいいんじゃないでしょうか。
2
2
 
3
-
4
-
5
- UIスレッドで動かす意味はないのでこちらなどでもいいでしょう。
3
+ UIスレッドで動かす意味はないのでこちらなどでもいいでしょう。
6
-
7
4
  [Timer クラス (System.Timers) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.timers.timer)
8
-
9
5
  [Timer クラス (System.Threading) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.threading.timer)
10
-
11
-
12
-
13
6
  こういったのもあります。
14
-
15
7
  [Observable.Timer Method (System.Reactive.Linq) | Microsoft Docs](https://docs.microsoft.com/en-us/previous-versions/dotnet/reactive-extensions/hh211753(v=vs.103))
16
-
17
-
18
8
 
19
9
  ---
20
10
 
21
-
22
-
23
11
  追記 実装例
24
-
25
12
  後片付け省略^^;
26
-
27
13
  それぞれ同じ意味にしたつもりですが、間違ってたらすいません^^;
28
-
29
- ```xaml
14
+ ```xml
30
-
31
15
  <Window
32
-
33
16
  x:Class="DispatcherTimerTest.MainWindow"
34
-
35
17
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
36
-
37
18
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
38
-
39
19
  xmlns:local="clr-namespace:DispatcherTimerTest"
40
-
41
20
  Width="800"
42
-
43
21
  Height="450"
44
-
45
22
  DataContext="{x:Static local:ViewModel.Instance}">
46
-
47
23
  <StackPanel>
48
-
49
24
  <Label Content="{Binding LabelContent1}" />
50
-
51
25
  <Label Content="{Binding LabelContent2}" />
52
-
53
26
  <Label Content="{Binding LabelContent3}" />
54
-
55
27
  <Label Content="{Binding LabelContent4}" />
56
-
57
28
  </StackPanel>
58
-
59
29
  </Window>
60
-
61
30
  ```
62
31
 
63
-
64
-
65
- ```C#
32
+ ```cs
66
-
67
33
  using System;
68
-
69
34
  using System.ComponentModel;
70
-
71
35
  using System.Reactive.Linq;
72
-
73
36
  using System.Runtime.CompilerServices;
74
37
 
75
-
76
-
77
38
  namespace DispatcherTimerTest
78
-
79
39
  {
80
-
81
40
  public class ViewModel : INotifyPropertyChanged
82
-
83
41
  {
84
-
85
42
  public static ViewModel Instance { get; } = new ViewModel();
86
43
 
87
44
 
88
-
89
-
90
-
91
45
  public int LabelContent1 { get => _LabelContent1; set => Set(ref _LabelContent1, value); }
92
-
93
46
  private int _LabelContent1;
94
47
 
95
-
96
-
97
48
  public int LabelContent2 { get => _LabelContent2; set => Set(ref _LabelContent2, value); }
98
-
99
49
  private int _LabelContent2;
100
50
 
101
-
102
-
103
51
  public int LabelContent3 { get => _LabelContent3; set => Set(ref _LabelContent3, value); }
104
-
105
52
  private int _LabelContent3;
106
53
 
107
-
108
-
109
54
  public int LabelContent4 { get => _LabelContent4; set => Set(ref _LabelContent4, value); }
110
-
111
55
  private int _LabelContent4;
112
56
 
113
-
114
-
115
57
  private System.Windows.Threading.DispatcherTimer dispatcherTimer;
116
-
117
58
  private System.Timers.Timer timersTimer;
118
-
119
59
  private System.Threading.Timer threadingTimer;
120
-
121
60
  private IDisposable observableTimer;
122
61
 
123
-
124
-
125
62
  public ViewModel()
126
-
127
63
  {
128
-
129
64
  dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
130
-
131
65
  dispatcherTimer.Interval = TimeSpan.FromMilliseconds(100);
132
-
133
66
  dispatcherTimer.Tick += (s, e) => LabelContent1 += 1;
134
-
135
67
  dispatcherTimer.Start();
136
68
 
137
69
 
138
-
139
-
140
-
141
70
  timersTimer = new System.Timers.Timer(100);
142
-
143
71
  timersTimer.Elapsed += (s, e) => LabelContent2 += 1;
144
-
145
72
  timersTimer.AutoReset = true;
146
-
147
73
  timersTimer.Enabled = true;
148
-
149
-
150
-
151
74
 
152
75
 
153
76
  threadingTimer = new System.Threading.Timer(x => LabelContent3 += 1, null, TimeSpan.FromMilliseconds(100), TimeSpan.FromMilliseconds(100));
154
77
 
155
78
 
156
-
157
-
158
-
159
79
  // NuGet System.Reactive.Linq
160
-
161
80
  observableTimer = Observable.Timer(TimeSpan.FromMilliseconds(100), TimeSpan.FromMilliseconds(100))
162
-
163
81
  .Subscribe(_ => LabelContent4 += 1);
164
-
165
-
166
82
 
167
83
  }
168
84
 
169
-
170
-
171
85
  #region INotifyPropertyChanged
172
-
173
86
  public event PropertyChangedEventHandler PropertyChanged;
174
-
175
87
  protected bool Set<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
176
-
177
88
  {
178
-
179
89
  if(Equals(storage, value)) return false;
180
90
 
181
-
182
-
183
91
  storage = value;
184
-
185
92
  OnPropertyChanged(propertyName);
186
-
187
93
  return true;
188
-
189
94
  }
190
-
191
95
  protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
192
-
193
96
  => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
194
-
195
97
  #endregion
196
-
197
98
  }
198
-
199
99
  }
200
-
201
100
  ```

1

追記 実装例

2020/06/30 10:14

投稿

TN8001
TN8001

スコア9326

test CHANGED
@@ -13,3 +13,189 @@
13
13
  こういったのもあります。
14
14
 
15
15
  [Observable.Timer Method (System.Reactive.Linq) | Microsoft Docs](https://docs.microsoft.com/en-us/previous-versions/dotnet/reactive-extensions/hh211753(v=vs.103))
16
+
17
+
18
+
19
+ ---
20
+
21
+
22
+
23
+ 追記 実装例
24
+
25
+ 後片付け省略^^;
26
+
27
+ それぞれ同じ意味にしたつもりですが、間違ってたらすいません^^;
28
+
29
+ ```xaml
30
+
31
+ <Window
32
+
33
+ x:Class="DispatcherTimerTest.MainWindow"
34
+
35
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
36
+
37
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
38
+
39
+ xmlns:local="clr-namespace:DispatcherTimerTest"
40
+
41
+ Width="800"
42
+
43
+ Height="450"
44
+
45
+ DataContext="{x:Static local:ViewModel.Instance}">
46
+
47
+ <StackPanel>
48
+
49
+ <Label Content="{Binding LabelContent1}" />
50
+
51
+ <Label Content="{Binding LabelContent2}" />
52
+
53
+ <Label Content="{Binding LabelContent3}" />
54
+
55
+ <Label Content="{Binding LabelContent4}" />
56
+
57
+ </StackPanel>
58
+
59
+ </Window>
60
+
61
+ ```
62
+
63
+
64
+
65
+ ```C#
66
+
67
+ using System;
68
+
69
+ using System.ComponentModel;
70
+
71
+ using System.Reactive.Linq;
72
+
73
+ using System.Runtime.CompilerServices;
74
+
75
+
76
+
77
+ namespace DispatcherTimerTest
78
+
79
+ {
80
+
81
+ public class ViewModel : INotifyPropertyChanged
82
+
83
+ {
84
+
85
+ public static ViewModel Instance { get; } = new ViewModel();
86
+
87
+
88
+
89
+
90
+
91
+ public int LabelContent1 { get => _LabelContent1; set => Set(ref _LabelContent1, value); }
92
+
93
+ private int _LabelContent1;
94
+
95
+
96
+
97
+ public int LabelContent2 { get => _LabelContent2; set => Set(ref _LabelContent2, value); }
98
+
99
+ private int _LabelContent2;
100
+
101
+
102
+
103
+ public int LabelContent3 { get => _LabelContent3; set => Set(ref _LabelContent3, value); }
104
+
105
+ private int _LabelContent3;
106
+
107
+
108
+
109
+ public int LabelContent4 { get => _LabelContent4; set => Set(ref _LabelContent4, value); }
110
+
111
+ private int _LabelContent4;
112
+
113
+
114
+
115
+ private System.Windows.Threading.DispatcherTimer dispatcherTimer;
116
+
117
+ private System.Timers.Timer timersTimer;
118
+
119
+ private System.Threading.Timer threadingTimer;
120
+
121
+ private IDisposable observableTimer;
122
+
123
+
124
+
125
+ public ViewModel()
126
+
127
+ {
128
+
129
+ dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
130
+
131
+ dispatcherTimer.Interval = TimeSpan.FromMilliseconds(100);
132
+
133
+ dispatcherTimer.Tick += (s, e) => LabelContent1 += 1;
134
+
135
+ dispatcherTimer.Start();
136
+
137
+
138
+
139
+
140
+
141
+ timersTimer = new System.Timers.Timer(100);
142
+
143
+ timersTimer.Elapsed += (s, e) => LabelContent2 += 1;
144
+
145
+ timersTimer.AutoReset = true;
146
+
147
+ timersTimer.Enabled = true;
148
+
149
+
150
+
151
+
152
+
153
+ threadingTimer = new System.Threading.Timer(x => LabelContent3 += 1, null, TimeSpan.FromMilliseconds(100), TimeSpan.FromMilliseconds(100));
154
+
155
+
156
+
157
+
158
+
159
+ // NuGet System.Reactive.Linq
160
+
161
+ observableTimer = Observable.Timer(TimeSpan.FromMilliseconds(100), TimeSpan.FromMilliseconds(100))
162
+
163
+ .Subscribe(_ => LabelContent4 += 1);
164
+
165
+
166
+
167
+ }
168
+
169
+
170
+
171
+ #region INotifyPropertyChanged
172
+
173
+ public event PropertyChangedEventHandler PropertyChanged;
174
+
175
+ protected bool Set<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
176
+
177
+ {
178
+
179
+ if(Equals(storage, value)) return false;
180
+
181
+
182
+
183
+ storage = value;
184
+
185
+ OnPropertyChanged(propertyName);
186
+
187
+ return true;
188
+
189
+ }
190
+
191
+ protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
192
+
193
+ => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
194
+
195
+ #endregion
196
+
197
+ }
198
+
199
+ }
200
+
201
+ ```