.NET异步方法调用的例子
生活随笔
收集整理的這篇文章主要介紹了
.NET异步方法调用的例子
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
這樣寫的好處是TestMethod在同步和異步線程下,都能順利地被調用.
MethodInvoker和Action都是.NET 2.0內置的Delegate類型,讓你方法地回調一個沒有參數的方法,而不用自己去定義新的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;
}
轉載于:https://www.cnblogs.com/top5/archive/2010/01/06/1640770.html
總結
以上是生活随笔為你收集整理的.NET异步方法调用的例子的全部內容,希望文章能夠幫你解決所遇到的問題。