ParallelProgramming Basic
a.Eachthread take data from collection , named them query chain .
b.execute
c.combinequery chain .
Take , TakeWhile , Skip , SkipWhilie ,Select ,SelectMany , ElementAt, Join , GoupBy, GroupJoin , Distinct , Intersect ,Except
Data for each thread will be independent(copied for each).
For simple operation (increase , decrease )
Var cancelSource = newCancellationTokenSource(); dataSource.AsParallelWithCancellation(cancelSource.Token); //whenever want to cancel execution , cancelSource.Cancel();
Force Application Start threads to execute .
1.ChunkThread
· If no index created on sequence , Bydefault , will use this one .
2.Rangeequal number
· Ifthe input sequenceis indexed (if it’san array orimplements IList<T>), PLINQchooses range partitioning.
· Otherwise, PLINQ chooses chunkpartitioning
3.Hash
CreateThread to execute , wait to complete .
Outerloop > Inner loop
loopState.Break
Start a newthread to do a task .
1.task.Wait();
2.task.Result
ContinueWith:
a.Onetask
b.Multipletask
Child Task(nested in another task)
a.WaitAny()
b.WaitAll()
c.Wait()
Task RunConditions :
a.NotOn Ran to completion
b.Noton faulted
c.Noton cancelled
Catch(AggregateExceptionaex){ Foreach(Exceptionex in aex.flatten().InnerExceptions){ Logger.Exception(ex); } }
TaskScheduler_uiScheduler; _uiScheduler= TaskScheduler.FromCurrentSynchronizationContext(); TaskFactory.StartNew<string>(servicemthod).continueWith(ant => lbl.Text = ant.Result,_uiScheduler);
Use TaskCompletion Source object to get result from another thread
Var source= new TaskCompletionSource<int>(); NewThread(()=>Thread.Sleep(5000);source.SetResult(123);).Start(); Var task=source.Task; Var result= task.Result;
Tasksproduce items => Task[] producer tasksconsume items => Task[] consumer Task.WaitAll(producer, consumer);