1.直接看代码
using System;
using System.Threading;
using System.Timers;
using System.Windows.Forms;
namespace WindowsFormsApp13 {
public partial class Form1 : Form {
static SynchronizationContext synt;
public Form1() {
synt = SynchronizationContext.Current;
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e) {
Thread th = new Thread(new ParameterizedThreadStart(otherPrint));
th.Start();
}
private void otherPrint(object obj) {
Control.CheckForIllegalCrossThreadCalls = false;
this.result.Text = obj as String;
Control.CheckForIllegalCrossThreadCalls = true;
}
private void print() {
Control.CheckForIllegalCrossThreadCalls = false;
this.result.Text = "123";
Control.CheckForIllegalCrossThreadCalls = true;
}
}
}
Thread requestHandler = new Thread(
() => {
。。。
}
);
requestHandler.Start();