回答編集履歴

1

追記

2017/10/10 07:17

投稿

Chironian
Chironian

スコア23272

test CHANGED
@@ -11,3 +11,119 @@
11
11
  例えば、Message_textBoxをサブスレッドで生成しているようなことはないでしょうか?
12
12
 
13
13
  ご提示されているソースを含むコントロール(Form1?)を生成したスレッドとは別のスレッドでMessage_textBoxを生成していた場合、そのようなエラーが出ます。
14
+
15
+
16
+
17
+ ---
18
+
19
+ 【追記】
20
+
21
+ Visual Studio 2017 Communityで、やってみました。
22
+
23
+ Form1にはMessage_textBoxだけ貼り付けてます。
24
+
25
+ 起動後、約3秒(300を3000に変えてます)でMessage_textBoxにtestと表示されます。
26
+
27
+
28
+
29
+ ```C#
30
+
31
+ using System;
32
+
33
+ using System.Collections.Generic;
34
+
35
+ using System.ComponentModel;
36
+
37
+ using System.Data;
38
+
39
+ using System.Drawing;
40
+
41
+ using System.Linq;
42
+
43
+ using System.Text;
44
+
45
+ using System.Threading;
46
+
47
+ using System.Threading.Tasks;
48
+
49
+ using System.Timers;
50
+
51
+ using System.Windows.Forms;
52
+
53
+
54
+
55
+ namespace WindowsFormsApp2
56
+
57
+ {
58
+
59
+ public partial class Form1 : Form
60
+
61
+ {
62
+
63
+ public Form1()
64
+
65
+ {
66
+
67
+ InitializeComponent();
68
+
69
+ // this.settingsData = Settings.Load();
70
+
71
+ timer = new System.Timers.Timer();
72
+
73
+ timer.Interval = 3000; // インターバル
74
+
75
+ timer.Elapsed += TimerHandler;
76
+
77
+ timer.AutoReset = true; // false:最初の1回だけ実行
78
+
79
+ //timer.Enabled = true; // timer.Start()と同じ
80
+
81
+ timer.Start();
82
+
83
+ }
84
+
85
+ private System.Timers.Timer timer = null;
86
+
87
+
88
+
89
+ private void TimerHandler(object sender, ElapsedEventArgs e)
90
+
91
+ {
92
+
93
+ Thread t = new Thread(new ThreadStart(anotherThread));
94
+
95
+ t.Start();
96
+
97
+ }
98
+
99
+
100
+
101
+ delegate void delegate1(String detail_data);
102
+
103
+
104
+
105
+ void setFocus(String detail_data)
106
+
107
+ {
108
+
109
+ Message_textBox.Text = detail_data;
110
+
111
+ Message_textBox.Focus();
112
+
113
+ }
114
+
115
+
116
+
117
+ void anotherThread()
118
+
119
+ {
120
+
121
+ Invoke(new delegate1(setFocus), "test");
122
+
123
+ }
124
+
125
+ }
126
+
127
+ }
128
+
129
+ ```