[C#] Lambda花式應用噁爛寫法(跨UI委派秒幹、多線程處理...etc)(轉)

  • 172
  • 0
  • 2018-12-11

https://www.30cm.tw/2015/04/c-lambdauietc.html

但今天用Lambda可以改寫: 

Thread nThread = new Thread(() => {/*多線程要做的事情*/});
nThread.Start();


甚至在噁心點寫的寫法: 

new Thread(() => {/*多線程要做的事情*/}).Start();
//效能更優化
Task.Run(() => MyTask());


https://www.30cm.tw/2015/04/c-lambdauietc.html

以下是其他功能

設定TPL最大最小值

System.Threading.ThreadPool.GetMinThreads(Int32, Int32)

https://docs.microsoft.com/en-us/dotnet/api/system.threading.threadpool.getminthreads?redirectedfrom=MSDN&view=netframework-4.7.2#System_Threading_ThreadPool_GetMinThreads_System_Int32__System_Int32__

多個task 且等候

List<Task> TaskList = new List<Task>();
foreach(...)
{
   var LastTask = new Task(SomeFunction);
   LastTask.Start();
   TaskList.Add(LastTask);
}

Task.WaitAll(TaskList.ToArray());

https://stackoverflow.com/questions/25009437/running-multiple-async-tasks-and-waiting-for-them-all-to-complete