1、简单的一般线程
Func<int, int, int> Add = (a, b) => a + b; Thread t = new Thread(() => { Console.WriteLine(Add(3,4)); }); t.IsBackground = true; t.Start();
2、线程池运行线程
Func<int, int, int> Add = (a, b) => a + b; ThreadPool.QueueUserWorkItem(s => { Console.WriteLine(Add(4, 5)); });
3、异步
(1)简单一点,在主线程中调用异步,然后在主线程中处理结果
Func<int, int, int> Add = (a, b) => a + b; var result = Add.BeginInvoke(3, 4, null, null); Console.WriteLine(Add.EndInvoke(result));
(2)在异步中执行结束后调用函数,并异步中执行处理结果。
Func<int, int, int> Add = (a, b) => a + b; Add.BeginInvoke(3, 4, myCallBack, Add);
private void myCallBack(IAsyncResult ar) { var del = (Func<int, int, int>)ar.AsyncState; var res=del.EndInvoke(ar); Console.WriteLine(res); }