The biggest new feature of C #5.0 is async and parallel.
In the past, in order to maintain a corresponding user interface, we can directly use Asynchronous delegation or system. threading namespace member, but system. threading. the tasks namespace provides a more concise method-using the task class. The task class can easily call methods in the next thread and can be used as a simple substitute for asynchronous delegation.
For details about async, refer toTask classAndAsynchronous Programming Using async and await (C # and Visual Basic)It introduces the methods and attributes, which are better than LZ.
The following is an example of implementing an asynchronous call to see if the usage is different.
What I want is: asynchronous execution method. After the method is executed, another method is executed asynchronously. The preceding method result is a parameter.
Using system; using system. threading. tasks; // asynchronous execution method. After the method is executed, another method is executed asynchronously. The previous result of this method is the namespace asynctask {class result {public int number {set; get ;}} class program {static void main (string [] ARGs) {var result = New Result {number = 4}; var T1 = new task (first, result ); // action var t2 = t1.continuewith <int> (first1); // func var T3 = t1.continuewith <int> (first2); t1.start (); console. writeline (t2.result); // T. result console. writeline (t3.result); console. readkey ();} static void first (Object O) {result x = (result) O; X. number * = 2;} static int first1 (Task O) {result x = (result) O. asyncstate; // return X. number * X. number;} static int first2 (Task O) {result x = (result) O. asyncstate; return X. number * X. number * X. number ;}}}
We can compare the debuglzq. Net asynchronous programming Summary-four implementation modes to see what the implementation methods are different if they are also asynchronous.
With regard to parallel, the dual-core (multi-core) computers are already widely used. Computers with multiple CPUs can execute threads in parallel, this will greatly improve the runtime performance of the application.
In general, the types in system. Threading. Tasks (and some related types in system. Threading) are called task parallel Library (TPL ). TPL uses the CLR thread pool to automatically allocate applications dynamically to available CPUs. TPL also handles work partitions, thread scheduling, status management, and other low-level details. The most important class in TPL is system. Threading. Tasks. Parallel, which provides a large number of methods to iterate data sets in parallel (implementing ienumberable <t> objects ). This class supports two major static methods: Parallel. For () and parallel. foreach () (parallel. Invoke (). Each method has many overloaded versions.
MsdnParallel class,Parallel Programming in. NET Framework For example
using System.Threading.Tasks;namespace ParallelFor{ class Test { static int N = 1000; static void TestMethod() { // Using a named method. Parallel.For(0, N, Method2); // Using an anonymous method. Parallel.For(0, N, delegate(int i) { // Do Work. }); // Using a lambda expression. Parallel.For(0, N, i => { // Do Work. }); } static void Method2(int i) { // Do work. } }}
Foreach and invoke are similar.
Parallel.ForEach(new[] {1, 2, 3, 4, 5}, i => Console.WriteLine("{0}, Task: {1}, Thread {2}", i, Task.CurrentId, Thread.CurrentThread.ManagedThreadId));
Parallel.Invoke( BasicAction, // Param #0 - static method () => // Param #1 - lambda expression { Console.WriteLine("Method=beta, Thread={0}", Thread.CurrentThread.ManagedThreadId); }, delegate() // Param #2 - in-line delegate { Console.WriteLine("Method=gamma, Thread={0}", Thread.CurrentThread.ManagedThreadId); } );
Finally, we can clearly explain that parallel and async are not part of the LZ blog. LZ is not actually used. I dare not say anything about it... blog is a personal understanding, which may inevitably lead to leaks. You are welcome to criticize and correct me! In terms of learning, Chinese books are generally lagging behind. Not all English books are translated for personal needs. In addition, the reasons for the translator's personal understanding and translation style are ultimately...
The company bought a new book:
The book is thick, with 1 K pages. The English book looks slow, but looks comfortable.