[C#]Invokeの引数の使い方とは?InvokeRequiredプロパティ
![[C#]Invokeの引数の使い方とは?InvokeRequiredプロパティ](/dotnet/column/wp-content/uploads/2020/04/web_director_01-960x320.jpg)
- SE
- Invokeの引数の使い方を教えてください。
- PM
- Invokeの引数の詳しい使い方を紹介します。
[C#]Invokeの引数の使い方とは?
今回は、C#でのInvokeの使い方について説明します。Invokeを使いたいケース、Invokeを使わずに別スレッドからコントロールを操作する間違った例、Invokeや引数の使い方、InvokeRequiredプロパティについて紹介します。
C#でのInvokeの使い方に興味のある方はぜひご覧ください。
Invokeを使いたいケース
C#のWindowsフォームからスレッドを作成した場合、そのスレッド(別スレッド)からコントロールを操作したいことがありますが、別スレッドから操作することはできません。Invokeを使用すると、別スレッドからコントロールを操作できるようになります。以降の章でInvokeについて紹介します。
Invokeを使わずに別スレッドからコントロールを操作する
C#において、Invokeを使わずに別スレッドからコントロールを操作する間違った例を紹介します。実際のソースコードを見てみましょう。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Threading;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
Button button1, button2;
Label label;
Thread thread;
public Form1()
{
this.AutoSize = true;
this.Load += Form1_Load;
}
private void Form1_Load(object sender, EventArgs e)
{
// ラベル
label = new Label();
label.Location = new Point(10, 10);
label.Text = "";
this.Controls.Add(label);
// ボタン
button1 = new Button();
button1.Location = new Point(10, 40);
button1.Text = "UI thread";
button1.Click += Button1_Click;
this.Controls.Add(button1);
// ボタン
button2 = new Button();
button2.Location = new Point(100, 40);
button2.Text = "Other thread";
button2.Click += Button2_Click;
this.Controls.Add(button2);
}
private void Button1_Click(object sender, EventArgs e)
{
label.Text = "UI thread";
}
private void Button2_Click(object sender, EventArgs e)
{
// スレッドを生成(引数に関数を指定)
thread = new Thread(new ThreadStart(ThreadFunc));
// スレッド開始
thread.Start();
}
private void ThreadFunc()
{
label.Text = "Other thread"; // InvalidOperationException
}
}
}
|
「UI thread」ボタンをクリックすると、labelが「UI thread」になります。「Other thread」ボタンをクリックすると、InvalidOperationExceptionが発生します。C#では、別スレッドからコントロールを操作することはできません。
Invokeの使い方
C#でのInvokeや引数の使い方について紹介します。実際のソースコードを見てみましょう。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Threading;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
Button button1, button2;
Label label;
Thread thread;
public Form1()
{
this.AutoSize = true;
this.Load += Form1_Load;
}
private void Form1_Load(object sender, EventArgs e)
{
// ラベル
label = new Label();
label.Location = new Point(10, 10);
label.Text = "";
this.Controls.Add(label);
// ボタン
button1 = new Button();
button1.Location = new Point(10, 40);
button1.Text = "UI thread";
button1.Click += Button1_Click;
this.Controls.Add(button1);
// ボタン
button2 = new Button();
button2.Location = new Point(100, 40);
button2.Text = "Other thread";
button2.Click += Button2_Click;
this.Controls.Add(button2);
}
private void Button1_Click(object sender, EventArgs e)
{
label.Text = "UI thread";
}
public delegate void LabelUpdateDelegate();
private void Button2_Click(object sender, EventArgs e)
{
// スレッドを生成(引数に関数を指定)
thread = new Thread(new ThreadStart(ThreadFunc));
// スレッド開始
thread.Start();
}
private void ThreadFunc()
{
// 引数にLabelUpdateDelegateを指定。LabelUpdateDelegateの引数にOnUpdateTextを指定。
Invoke(new LabelUpdateDelegate(OnUpdateText));
}
private void OnUpdateText()
{
label.Text = "Other thread";
}
}
}
|
「UI thread」ボタンをクリックすると、labelが「UI thread」になります。「Other thread」ボタンをクリックすると、labelが「Other thread」になります。
このようにC#では、Invokeを使うことで、別スレッドからコントロールを操作できます。しかし、label更新のためだけにOnUpdateTextメソッドが存在している状態です。
InvokeRequiredプロパティ
C#でのInvokeRequiredプロパティの使い方を紹介します。labelを更新するOnUpdateTextメソッドをまとめ、別スレッドから呼び出された場合はInvokeで処理します。実際のソースコードを見てみましょう。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Threading;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
Button button1, button2;
Label label;
Thread thread;
string message;
public Form1()
{
this.AutoSize = true;
this.Load += Form1_Load;
}
private void Form1_Load(object sender, EventArgs e)
{
message = "";
// ラベル
label = new Label();
label.Location = new Point(10, 10);
label.Text = "";
this.Controls.Add(label);
// ボタン
button1 = new Button();
button1.Location = new Point(10, 40);
button1.Text = "UI thread";
button1.Click += Button1_Click;
this.Controls.Add(button1);
// ボタン
button2 = new Button();
button2.Location = new Point(100, 40);
button2.Text = "Other thread";
button2.Click += Button2_Click;
this.Controls.Add(button2);
}
private void Button1_Click(object sender, EventArgs e)
{
message = "UI thread";
OnUpdateText();
}
public delegate void LabelUpdateDelegate();
private void Button2_Click(object sender, EventArgs e)
{
// スレッドを生成(引数に関数を指定)
thread = new Thread(new ThreadStart(ThreadFunc));
// スレッド開始
thread.Start();
}
private void ThreadFunc()
{
OnUpdateText();
}
private void OnUpdateText()
{
if (InvokeRequired)
{
message = "Other thread";
// 引数にLabelUpdateDelegateを指定。LabelUpdateDelegateの引数にOnUpdateTextを指定。
Invoke(new LabelUpdateDelegate(OnUpdateText));
return;
}
label.Text = message;
}
}
}
|
「UI thread」ボタンをクリックすると、labelが「UI thread」になります。「Other thread」ボタンをクリックすると、labelが「Other thread」になります。
- SE
- C#でのInvokeがわかりました。
- PM
- C#でのInvokeの間違った例なども紹介しました。Invokeの知識を深めて実際にコードを書いてみましょう。
まとめ
いかがでしたでしょうか。C#でのInvokeの使い方について説明しました。Invokeを使いたいケース、Invokeを使わずに別スレッドからコントロールを操作する間違った例、Invokeや引数の使い方、InvokeRequiredプロパティについて紹介しました。
ぜひご自身でC#のソースコードを書いて、理解を深めてください。
Search キーワード検索
Popular 人気の記事
-
【VB.NET入門】DataGridViewの使い方まとめ
公開: 更新:
reccomended おすすめ記事
-
【.NETが統合】.NET 5の概要と今後のリリース予定
公開: 更新:
Categories 連載一覧
Tags タグ一覧
Jobs 新着案件
-
システム開発/東京都新宿区/【WEB面談可/C#経験者/20代前半の方活躍中/経験1年以上の方活躍中】/在宅勤務
月給29万~34万円東京都新宿区(新宿駅) -
システム開発/東京都新宿区/【WEB面談可/C#経験者/20代後半~40代の方活躍中/経験年数不問】/在宅勤務
月給41万~50万円東京都新宿区(新宿駅) -
デバック、テスト項目の作成/神奈川県横浜市/【WEB面談可/C#経験者/20代前半の方活躍中/経験1年以上の方活躍中】/在宅勤務
月給29万~34万円神奈川県横浜市(桜木町駅) -
デバック、テスト項目の作成/神奈川県横浜市/【WEB面談可/C#経験者/20代後半~40代の方活躍中/経験年数不問】/在宅勤務
月給41万~50万円神奈川県横浜市(桜木町駅) -
基幹システム開発導入/東京都新宿区/【WEB面談可/C#経験者/20代前半の方活躍中/経験1年以上の方活躍中】/在宅勤務
月給29万~34万円東京都新宿区(西新宿駅) -
基幹システム開発導入/東京都新宿区/【WEB面談可/C#経験者/20代後半~40代の方活躍中/経験年数不問】/在宅勤務
月給41万~50万円東京都新宿区(西新宿駅)