質問編集履歴
5
修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -236,7 +236,7 @@
|
|
236
236
|
|
237
237
|
|
238
238
|
|
239
|
-
|
239
|
+
問題解決後のソースコード
|
240
240
|
|
241
241
|
```
|
242
242
|
|
4
修正後のソースを追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -234,6 +234,214 @@
|
|
234
234
|
|
235
235
|
```
|
236
236
|
|
237
|
+
|
238
|
+
|
239
|
+
修正後
|
240
|
+
|
241
|
+
```
|
242
|
+
|
243
|
+
using System;
|
244
|
+
|
245
|
+
using System.Collections.Generic;
|
246
|
+
|
247
|
+
using System.ComponentModel;
|
248
|
+
|
249
|
+
using System.Data;
|
250
|
+
|
251
|
+
using System.Drawing;
|
252
|
+
|
253
|
+
using System.Linq;
|
254
|
+
|
255
|
+
using System.Text;
|
256
|
+
|
257
|
+
using System.Threading.Tasks;
|
258
|
+
|
259
|
+
using System.Windows.Forms;
|
260
|
+
|
261
|
+
|
262
|
+
|
263
|
+
using System.Net;
|
264
|
+
|
265
|
+
using System.IO;
|
266
|
+
|
267
|
+
using System.Text.RegularExpressions;
|
268
|
+
|
269
|
+
|
270
|
+
|
271
|
+
namespace Asynctest
|
272
|
+
|
273
|
+
{
|
274
|
+
|
275
|
+
public partial class Form1 : Form
|
276
|
+
|
277
|
+
{
|
278
|
+
|
279
|
+
public Form1()
|
280
|
+
|
281
|
+
{
|
282
|
+
|
283
|
+
InitializeComponent();
|
284
|
+
|
285
|
+
}
|
286
|
+
|
287
|
+
|
288
|
+
|
289
|
+
private async void Button1_Click(object sender, EventArgs e)
|
290
|
+
|
291
|
+
{
|
292
|
+
|
293
|
+
const string url = @"http://192.168.0.99/";
|
294
|
+
|
295
|
+
|
296
|
+
|
297
|
+
// スレッドを起動し、以降のコードを実行する前に呼び出し元へ戻る
|
298
|
+
|
299
|
+
await Task.Run(() =>
|
300
|
+
|
301
|
+
{
|
302
|
+
|
303
|
+
//非同期で実行したい重い処理
|
304
|
+
|
305
|
+
//Task.Delay(1000).Wait();
|
306
|
+
|
307
|
+
byte[] data;
|
308
|
+
|
309
|
+
WebRequest req = null;
|
310
|
+
|
311
|
+
WebResponse res = null;
|
312
|
+
|
313
|
+
string str = null;
|
314
|
+
|
315
|
+
|
316
|
+
|
317
|
+
try
|
318
|
+
|
319
|
+
{
|
320
|
+
|
321
|
+
req = WebRequest.Create(url);
|
322
|
+
|
323
|
+
res = req.GetResponse();
|
324
|
+
|
325
|
+
Stream st = res.GetResponseStream();
|
326
|
+
|
327
|
+
data = ReadBinaryData(st);
|
328
|
+
|
329
|
+
str = System.Text.Encoding.GetEncoding("csISO2022JP").GetString(data);
|
330
|
+
|
331
|
+
}
|
332
|
+
|
333
|
+
catch (Exception ex)
|
334
|
+
|
335
|
+
{
|
336
|
+
|
337
|
+
Console.WriteLine(ex.Message);
|
338
|
+
|
339
|
+
|
340
|
+
|
341
|
+
//UI要素へのアクセス(非同期で実行したい処理と混ぜずに、単独で行う。)
|
342
|
+
|
343
|
+
this.Invoke(new Action(() => {
|
344
|
+
|
345
|
+
listBox1.Items.Insert(listBox1.Items.Count, ex.Message);
|
346
|
+
|
347
|
+
}));
|
348
|
+
|
349
|
+
return;
|
350
|
+
|
351
|
+
}
|
352
|
+
|
353
|
+
finally
|
354
|
+
|
355
|
+
{
|
356
|
+
|
357
|
+
if (res != null) res.Close();
|
358
|
+
|
359
|
+
}
|
360
|
+
|
361
|
+
//UI要素へのアクセス(非同期で実行したい処理と混ぜずに、単独で行う。)
|
362
|
+
|
363
|
+
this.Invoke(new Action(() => {
|
364
|
+
|
365
|
+
listBox1.Items.Insert(listBox1.Items.Count, str);
|
366
|
+
|
367
|
+
}));
|
368
|
+
|
369
|
+
|
370
|
+
|
371
|
+
});
|
372
|
+
|
373
|
+
|
374
|
+
|
375
|
+
}
|
376
|
+
|
377
|
+
|
378
|
+
|
379
|
+
static byte[] ReadBinaryData(Stream st)
|
380
|
+
|
381
|
+
{
|
382
|
+
|
383
|
+
byte[] buf = new byte[32768];
|
384
|
+
|
385
|
+
using (MemoryStream ms = new MemoryStream())
|
386
|
+
|
387
|
+
{
|
388
|
+
|
389
|
+
while (true)
|
390
|
+
|
391
|
+
{
|
392
|
+
|
393
|
+
int r = st.Read(buf, 0, buf.Length);
|
394
|
+
|
395
|
+
|
396
|
+
|
397
|
+
//一時バッファの内容をメモリ・ストリームに書き込む
|
398
|
+
|
399
|
+
if (r > 0)
|
400
|
+
|
401
|
+
{
|
402
|
+
|
403
|
+
ms.Write(buf, 0, r);
|
404
|
+
|
405
|
+
}
|
406
|
+
|
407
|
+
else
|
408
|
+
|
409
|
+
{
|
410
|
+
|
411
|
+
break;
|
412
|
+
|
413
|
+
}
|
414
|
+
|
415
|
+
}
|
416
|
+
|
417
|
+
return ms.ToArray();
|
418
|
+
|
419
|
+
}
|
420
|
+
|
421
|
+
}
|
422
|
+
|
423
|
+
|
424
|
+
|
425
|
+
private void Button2_Click(object sender, EventArgs e)
|
426
|
+
|
427
|
+
{
|
428
|
+
|
429
|
+
listBox1.Items.Insert(listBox1.Items.Count, "!");
|
430
|
+
|
431
|
+
|
432
|
+
|
433
|
+
}
|
434
|
+
|
435
|
+
}
|
436
|
+
|
437
|
+
}
|
438
|
+
|
439
|
+
```
|
440
|
+
|
441
|
+
|
442
|
+
|
443
|
+
|
444
|
+
|
237
445
|
### 環境
|
238
446
|
|
239
447
|
Microsoft Windows 10 Pro (Version 1809)
|
3
修正
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
重い処理の処理中に、UI操作を行いたいのですが、処理がブロックされ上手く行きません。
|
test
CHANGED
File without changes
|
2
修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
### 前提・実現したいこと
|
2
2
|
|
3
|
-
|
3
|
+
重い処理の処理中に、UI操作を行いたいのですが、処理がブロックされ上手く行きません。
|
4
4
|
|
5
5
|
|
6
6
|
|
1
補足
test
CHANGED
File without changes
|
test
CHANGED
@@ -24,6 +24,12 @@
|
|
24
24
|
|
25
25
|
下に画面イメージとソースコードを載せておきます。
|
26
26
|
|
27
|
+
左のボタンがWEBページにアクセスするボタン
|
28
|
+
|
29
|
+
右のボタンがUI操作を確認するためのボタンです。
|
30
|
+
|
31
|
+
|
32
|
+
|
27
33
|
ソースコードは、Windows Formsアプリ、及びC#です。
|
28
34
|
|
29
35
|

|
@@ -84,7 +90,7 @@
|
|
84
90
|
|
85
91
|
{
|
86
92
|
|
87
|
-
const string url = @"http://192.168.0.99/";
|
93
|
+
const string url = @"http://192.168.0.99/"; //レスポンスを遅くするために、意図的にこのURLにアクセス
|
88
94
|
|
89
95
|
|
90
96
|
|
@@ -94,6 +100,8 @@
|
|
94
100
|
|
95
101
|
{
|
96
102
|
|
103
|
+
//UIスレッドにアクセスするためにInvokeを使用
|
104
|
+
|
97
105
|
this.Invoke(new Action(() =>
|
98
106
|
|
99
107
|
{
|
@@ -206,6 +214,8 @@
|
|
206
214
|
|
207
215
|
|
208
216
|
|
217
|
+
//リストボックスに1行追加するだけ
|
218
|
+
|
209
219
|
private void Button2_Click(object sender, EventArgs e)
|
210
220
|
|
211
221
|
{
|