[C#]Invokeの引数の使い方とは?InvokeRequiredプロパティ
![[C#]Invokeの引数の使い方とは?InvokeRequiredプロパティ](https://www.fenet.jp/dotnet/column/wp-content/uploads/2020/04/web_director_01.jpg)
エンジニア
マネージャー
[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」になります。
エンジニア
マネージャー
まとめ
いかがでしたでしょうか。C#でのInvokeの使い方について説明しました。Invokeを使いたいケース、Invokeを使わずに別スレッドからコントロールを操作する間違った例、Invokeや引数の使い方、InvokeRequiredプロパティについて紹介しました。
ぜひご自身でC#のソースコードを書いて、理解を深めてください。
FEnet.NETナビ・.NETコラムは株式会社オープンアップシステムが運営しています。
株式会社オープンアップシステムはこんな会社です
秋葉原オフィスには株式会社オープンアップシステムをはじめグループのIT企業が集結!
数多くのエンジニアが集まります。

-
スマホアプリから業務系システムまで
スマホアプリから業務系システムまで開発案件多数。システムエンジニア・プログラマーとしての多彩なキャリアパスがあります。
-
充実した研修制度
毎年、IT技術のトレンドや社員の要望に合わせて、カリキュラムを刷新し展開しています。社内講師の丁寧なサポートを受けながら、自分のペースで学ぶことができます。
-
資格取得を応援
スキルアップしたい社員を応援するために資格取得一時金制度を設けています。受験料(実費)と合わせて資格レベルに合わせた最大10万円の一時金も支給しています。
-
東証プライム上場企業グループ
オープンアップシステムは東証プライム上場「株式会社オープンアップグループ」のグループ企業です。
安定した経営基盤とグループ間のスムーズな連携でコロナ禍でも安定した雇用を実現させています。
株式会社オープンアップシステムに興味を持った方へ
株式会社オープンアップシステムでは、開発系エンジニア・プログラマを募集しています。
年収をアップしたい!スキルアップしたい!大手の上流案件にチャレンジしたい!
まずは話だけでも聞いてみたい場合もOK。お気軽にご登録ください。


C#新着案件New Job
システム開発/東京都新宿区/【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万円東京都新宿区(西新宿駅)