.NET异步方法调用的例子
生活随笔
收集整理的這篇文章主要介紹了
.NET异步方法调用的例子
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
這樣寫的好處是TestMethod在同步和異步線程下,都能順利地被調(diào)用.
MethodInvoker和Action都是.NET 2.0內(nèi)置的Delegate類型,讓你方法地回調(diào)一個沒有參數(shù)的方法,而不用自己去定義新的Delegate.
private void button1_Click(object sender, EventArgs e){
Thread t = new Thread(new ThreadStart(TestMethod));
t.Start();
}
public void TestMethod()
{
if (this.InvokeRequired)
{
//MethodInvoker handler = new MethodInvoker(TestMethod);
Action handler = new Action(TestMethod);
this.Invoke(handler, null);
}
else
{
this.Text = "Async Invoked.";
MessageBox.Show("Async Invoked");
}
} public void Calc(int a, int b, int c, int d)
{
var r = a + b + c + d;
}
轉(zhuǎn)載于:https://www.cnblogs.com/top5/archive/2010/01/06/1640770.html
總結(jié)
以上是生活随笔為你收集整理的.NET异步方法调用的例子的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 近段总结
- 下一篇: Firefox, IE等不同浏览器对Ja