private void Form1_Load(object sender, EventArgs e)
{
///控件属性来处理
Control.CheckForIllegalCrossThreadCalls = false;
}
DataSet ds = new DataSet();
Mydelegate del = new Mydelegate(LoadData);
IAsyncResult result = del.BeginInvoke(null, null);
ds = del.EndInvoke(result);
gridControl1.DataSource = ds.Tables[0];
this.Invoke((EventHandler)delegate
{
labMessage.Text = "数据填充中......";
});
/// <summary>
/// Invoke 修改主线程控件的属性
/// </summary>
/// <param name="strMsg"></param>
delegate void SafeSetText(string strMsg);
private void SetText(string strMsg)
{
if (textBox1.InvokeRequired)
{
SafeSetText objSet = new SafeSetText(SetText);
textBox1.Invoke(objSet, new object[] { strMsg });
}
else
{
textBox1.Text = strMsg;
}
}
/// <summary>
/// 匿名委托
/// </summary>
/// <param name="strMsg"></param>
delegate void SafeSetText(string strMsg);
private void SetText2(string strMsg)
{
SafeSetText objSet = delegate(string str)
{
textBox1.Text = str;
};
textBox1.Invoke(objSet, new object[] { strMsg });
}
///3.0及以后的版本中有了Lamda表达式
this.Invoke(new Action(() =>
{
textBox1.Text = "关闭";
}));
Thread invokeThread;
private void button1_Click(object sender, EventArgs e)
{
////执行输出顺序:AAA CCC EEE BBB DDD
string DataTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ff");
MessageBox.Show(Thread.CurrentThread.GetHashCode().ToString() + "AAA" + " " + DataTime);
invokeThread = new Thread(new ThreadStart(StartMethod));
invokeThread.Start();
string a = string.Empty;
for (int i = 0; i < 3; i++) //调整循环次数,看的会更清楚
{
//Thread.Sleep(1000);
a = a + "B";
}
string DataTime2= DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ff");
MessageBox.Show(Thread.CurrentThread.GetHashCode().ToString() + a + " " + DataTime2);
}
private void StartMethod()
{
string DataTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ff");
MessageBox.Show(Thread.CurrentThread.GetHashCode().ToString() + "CCC" + " " + DataTime);
button1.Invoke(new Action(invokeMethod),new Object[]{});
string DataTime2 = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ff");
MessageBox.Show(Thread.CurrentThread.GetHashCode().ToString() + "DDD" + " " + DataTime2);
}
private void invokeMethod()
{
//Thread.Sleep(3000);
string DataTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ff");
MessageBox.Show(Thread.CurrentThread.GetHashCode().ToString() + "EEE" + " " + DataTime);
}
Thread begingInvoke;
private void button2_Click(object sender, EventArgs e)
{
string DataTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ff");
MessageBox.Show(Thread.CurrentThread.GetHashCode().ToString() + "AAA" + " " + DataTime);
begingInvoke = new Thread(new ThreadStart(begingInvokeStartMethod));
begingInvoke.Start();
string a = string.Empty;
for (int i = 0; i < 3; i++) //调整循环次数,看的会更清楚
{
//Thread.Sleep(1000);
a = a + "B";
}
string DataTime2 = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ff");
MessageBox.Show(Thread.CurrentThread.GetHashCode().ToString() + a + " " + DataTime2);
}
private void begingInvokeStartMethod()
{
string DataTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ff");
MessageBox.Show(Thread.CurrentThread.GetHashCode().ToString() + "CCC" + " " + DataTime);
button1.BeginInvoke(new Action(begingInvokeMethod),new Object[]{});
string DataTime2 = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ff");
MessageBox.Show(Thread.CurrentThread.GetHashCode().ToString() + "DDD" + " " + DataTime2);
}
private void begingInvokeMethod()
{
string DataTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ff");
MessageBox.Show(Thread.CurrentThread.GetHashCode().ToString() + "EEE" + " " + DataTime);
}