2. The sub-thread obtains the data of the main form control. The TextBox is used as an example.
Using System;
Using System. Collections. Generic;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Text;
Using System. Windows. Forms;
Using System. Threading;
Namespace WindowsApplication1
{
Public partial class Form1: Form
{
Private System. Windows. Forms. TextBox textBox1;
Private System. Windows. Forms. Button button1;
Public Form1 ()
{
InitializeComponent ();
}
Private void InitializeComponent ()
{
This. textBox1 = new System. Windows. Forms. TextBox ();
This. button1 = new System. Windows. Forms. Button ();
This. SuspendLayout ();
//
// TextBox1
//
This. textBox1.Location = new System. Drawing. Point (111, 44 );
This. textBox1.Name = "textBox1 ";
This. textBox1.Text = "aaaaaaaaaa ";
This. textBox1.Size = new System. Drawing. Size (194, 21 );
This. textBox1.TabIndex = 0;
//
// Button1
//
This. button1.Location = new System. Drawing. Point (325, 34 );
This. button1.Name = "button1 ";
This. button1.Size = new System. Drawing. Size (87, 30 );
This. button1.TabIndex = 1;
This. button1.Text = "button1 ";
This. button1.UseVisualStyleBackColor = true;
This. button1.Click + = new System. EventHandler (this. button#click );
//
// Form1
//
This. AutoScaleDimensions = new System. Drawing. SizeF (6F, 12F );
This. AutoScaleMode = System. Windows. Forms. AutoScaleMode. Font;
This. ClientSize = new System. Drawing. Size (471,353 );
This. Controls. Add (this. button1 );
This. Controls. Add (this. textBox1 );
This. Name = "Form1 ";
This. Text = "Form1 ";
This. ResumeLayout (false );
This. initialize mlayout ();
}
Private void button#click (object sender, EventArgs e)
{
Thread t = new Thread (ThreadStart );
T. Start ();
}
Private void ThreadStart ()
{
MessageBox. Show (GetTextBoxText ());
}
Private delegate string GetTextBoxTextDelegate ();
Private string GetTextBoxText ()
{
If (this. textBox1.InvokeRequired)
{
Return this. Invoke (new GetTextBoxTextDelegate (GetTextBoxText). ToString ();
}
Else
{
Return this. textBox1.Text;
}
}
}
}