回答編集履歴

1

見直しキャンペーン中

2023/07/21 12:59

投稿

TN8001
TN8001

スコア9862

test CHANGED
@@ -1,195 +1,98 @@
1
1
  ##### エラー原因
2
-
3
2
  そこに`var`は書けません。
4
3
 
5
-
6
-
7
4
  ##### 他問題点
8
-
9
5
  `Task.Delay()`に`CancellationToken`を渡さないと意味がないです。
10
-
11
6
  `CancellationTokenSource`は、キャンセル後は使いまわせないので作り直します。
12
7
 
13
-
14
-
15
- ```C#
8
+ ```cs
16
-
17
9
  using System;
18
-
19
10
  using System.Diagnostics;
20
-
21
11
  using System.Threading;
22
-
23
12
  using System.Threading.Tasks;
24
-
25
13
  using System.Windows.Forms;
26
14
 
27
-
28
-
29
15
  namespace Questions259056
30
-
31
16
  {
32
-
33
17
  public partial class Timer1 : Form
34
-
35
18
  {
36
-
37
19
  private readonly Stopwatch sw = new Stopwatch();
38
-
39
20
  private int i;
40
-
41
21
  private CancellationTokenSource tokenSource;
42
-
43
-
44
22
 
45
23
  public Form1() => InitializeComponent();
46
24
 
47
-
48
-
49
25
  private void timer_Tick1(object sender, EventArgs e)
50
-
51
26
  => labelTime.Text = sw.Elapsed.ToString(@"mm\:ss.fff");
52
-
53
27
 
54
-
55
28
  private async Task Beep8(CancellationToken token)
56
-
57
29
  {
58
-
59
30
  await Task.Delay(8000, token);
60
-
61
31
  Console.Beep();
62
-
32
+ }
33
+ private async Task Beep9(CancellationToken token)
34
+ {
35
+ await Task.Delay(9000, token);
36
+ Console.Beep();
37
+ }
38
+ private async Task Sound(CancellationToken token)
39
+ {
40
+ await Task.Delay(10000, token);
41
+ Console.Beep(800, 1000);
63
42
  }
64
43
 
65
- private async Task Beep9(CancellationToken token)
44
+ private void Timer1_Click(object sender, EventArgs e)
66
-
67
45
  {
68
-
69
- await Task.Delay(9000, token);
70
-
71
- Console.Beep();
72
-
73
- }
74
-
75
- private async Task Sound(CancellationToken token)
76
-
77
- {
78
-
79
- await Task.Delay(10000, token);
80
-
81
- Console.Beep(800, 1000);
82
-
83
- }
84
-
85
-
86
-
87
- private void Timer1_Click(object sender, EventArgs e)
88
-
89
- {
90
-
91
46
  i++;
92
-
93
47
  if(i == 1)
94
-
95
48
  {
96
-
97
49
  Console.Beep();
98
-
99
50
  sw.Restart();
100
-
101
51
  tick1.Enabled = true;
102
52
 
53
+ tokenSource = new CancellationTokenSource();
54
+ var token = tokenSource.Token;
55
+ Task.Run(() => Beep8(token));
56
+ Task.Run(() => Beep9(token));
57
+ Task.Run(() => Sound(token));
58
+ }
103
59
 
60
+ if(i == 2)
61
+ {
62
+ sw.Stop();
63
+ tick1.Enabled = false;
64
+ tokenSource.Cancel();
65
+ labelFtime1_1.Text = sw.Elapsed.ToString(@"mm\:ss.fff");
66
+ labelTime.Text = sw.Elapsed.ToString(@"mm\:ss.fff");
67
+ }
68
+
69
+ if(i == 3)
70
+ {
71
+ Console.Beep();
72
+ sw.Restart();
73
+ tick1.Enabled = true;
104
74
 
105
75
  tokenSource = new CancellationTokenSource();
106
-
107
76
  var token = tokenSource.Token;
108
-
109
77
  Task.Run(() => Beep8(token));
110
-
111
78
  Task.Run(() => Beep9(token));
112
-
113
79
  Task.Run(() => Sound(token));
114
-
115
80
  }
116
81
 
117
-
118
-
119
- if(i == 2)
82
+ if(i == 4)
120
-
121
83
  {
122
-
123
- sw.Stop();
124
-
125
- tick1.Enabled = false;
126
-
127
- tokenSource.Cancel();
128
-
129
- labelFtime1_1.Text = sw.Elapsed.ToString(@"mm\:ss.fff");
84
+ labelFtime1_2.Text = sw.Elapsed.ToString(@"mm\:ss.fff");
130
-
131
- labelTime.Text = sw.Elapsed.ToString(@"mm\:ss.fff");
132
-
133
85
  }
134
86
 
135
-
136
-
137
- if(i == 3)
87
+ if(i == 5)
138
-
139
88
  {
140
-
141
- Console.Beep();
142
-
143
- sw.Restart();
89
+ sw.Stop();
144
-
145
- tick1.Enabled = true;
90
+ tick1.Enabled = false;
146
-
147
-
148
-
149
- tokenSource = new CancellationTokenSource();
91
+ tokenSource.Cancel();
150
-
151
- var token = tokenSource.Token;
92
+ labelGtime1.Text = sw.Elapsed.ToString(@"mm\:ss.fff");
152
-
153
- Task.Run(() => Beep8(token));
93
+ labelTime.Text = sw.Elapsed.ToString(@"mm\:ss.fff");
154
-
155
- Task.Run(() => Beep9(token));
156
-
157
- Task.Run(() => Sound(token));
158
-
159
94
  }
160
-
161
-
162
-
163
- if(i == 4)
164
-
165
- {
166
-
167
- labelFtime1_2.Text = sw.Elapsed.ToString(@"mm\:ss.fff");
168
-
169
- }
170
-
171
-
172
-
173
- if(i == 5)
174
-
175
- {
176
-
177
- sw.Stop();
178
-
179
- tick1.Enabled = false;
180
-
181
- tokenSource.Cancel();
182
-
183
- labelGtime1.Text = sw.Elapsed.ToString(@"mm\:ss.fff");
184
-
185
- labelTime.Text = sw.Elapsed.ToString(@"mm\:ss.fff");
186
-
187
- }
188
-
189
95
  }
190
-
191
96
  }
192
-
193
97
  }
194
-
195
98
  ```