1.委托:
public delegate int Math(int param1,int param2);定义委托类型
Public int Add(int param1,int param2)//定义同签名函数
{
Return param1+param2;
}
Math math;//声明委托
math=new Math(Add);创建委托对象,与指定进行关联
math(3,4);//调用委托函数
2.官方为了方便 给了Func Action
Func<int,int,int> math=delegate(int param1,int param2)
{
Return param1+param2;
}
Lambda:
Func<int,int,int> math=(param1,param2)=>
{
Return param1+param2;
}
Action的使用如同上面Func的使用一样,没有返回值
Public void Add(int param1,int param2)
{
MessageBox.show((param1+param2).ToString());
}
Action<int,int> math=Add;
math(3,4);
public static HomePageResponse HomePage(HomePageRequest request)
{
Task taskGetUnread = Task.Factory.StartNew(() => GetUnReadStatus(businesstypes, zjid, response));
Task taskGetRecommend = Task.Factory.StartNew(() => GetRecommend(response));
Task taskGetRunningStatus = Task.Factory.StartNew(
() => GetRunningStatus(vehicleid, ref runningstatus, ref accumulatorstatus, ref residualoil, ref accumulatorvalue, response, ref getvaluetime));
Task<VehicleDiagnosesData> taskDiagnose = Task.Factory.StartNew<VehicleDiagnosesData>(() => GetDiagnosesData(vehicleid, devicetype, response));
Task<VehicleAffairsData> taskAffairs = Task.Factory.StartNew<VehicleAffairsData>(() => GetOverView(vehicleid, zjid, response));
Task<VehicleDriveData> taskDriveData = Task.Factory.StartNew<VehicleDriveData>(
() => GetTripAnalysis(vehicleid, devicetype, rtripid, ref platenumber, ref forrealtime, response));
Task[] tasks = new Task[] { taskGetUnread, taskGetRecommend, taskGetRunningStatus, taskDiagnose, taskAffairs, taskDriveData};
Task.WaitAll(tasks);
WaitAll 是阻塞当前线程等待其它任务完毕的意思;
WhenAll
}
https://docs.microsoft.com/zh-cn/dotnet/api/system.threading.tasks.task.waitall?view=netframework-4.7.2
1、Task用的是线程池,线程池的线程数量的有上限的,这个可以通过ThreadPool修改
int minWorkThreadCount = processorCount * 4;
int minIOWorkThreadCount = processorCount * 2;
ThreadPool.SetMinThreads(minWorkThreadCount, minIOWorkThreadCount);
2、Task.Factory.StartNew不是直接创建线程,创建的是任务,它有一个任务队列,然后通过任务调度器把任务分配到线程池中的空闲线程中,如果任务的数量比线程池中的线程多,线程池的线程数量还没有到达上限,就会创建新线程执行任务。如果线程池的线程已到达上限,没有分配到线程的任务需要等待有线程空闲的时候才执行。
来源:oschina
链接:https://my.oschina.net/u/4281713/blog/3786406