上图的所有多线程案例大全完整版本项目源代码下载---->点击下载
private void DoingSomethin(string name)
{
for (int i = 0; i < 20; i++)
{
Thread.Sleep(100);
}
Console.WriteLine($"{name}-->{Thread.CurrentThread.ManagedThreadId}");
}
private void button4_Click(object sender, EventArgs e)
{
Action<string> action = this.DoingSomethin;
IAsyncResult asyncResult = null; //返回内容
//线程结束后回调函数
AsyncCallback callbak = ia =>
{
Console.WriteLine(object.ReferenceEquals(asyncResult, ia));
Console.WriteLine(ia.AsyncState);//参数内容
Console.WriteLine($"运行这里已经完成-->{Thread.CurrentThread.ManagedThreadId}");
};
//异步
asyncResult = action.BeginInvoke("button4_Click", callbak, "hello");
}