I. thread World before the FrameWork 4.0
Before the. NET FrameWork 4.0, if we use threads. Generally there are several ways to do this:
- Using the System.Threading.Thread class, call the instance method start () to open a new thread and call the Abort () method to terminate the thread prematurely.
- Using the System.Threading.ThreadPool class, call the static method QueueUserWorkItem (), put the method into the thread pool queue, and the thread pool to control the call.
- Use a series of asynchronous methods such as Begininvoke,endinvoke,beginread,enread,beginwrite,endwrite.
- Using the System.ComponentModel.BackgroundWorker control, call the instance method RunWorkerAsync () to open a new thread.
Two. . Net legacy Asynchronous Programming Overview
- Asynchronous programming Model (APM) in which asynchronous operations are represented by a pair of begin/end methods, such as Filestream.beginread and Stream.endread.
- Event-based Asynchronous mode (EAP) in which asynchronous operations are performed by method/event pairs called "Operation name Async" and "Operation name completed" (for example, Webclient.downloadstringasync and webclient.downloadstringcompleted) said. (EAP is introduced in the. NET Framework version 2.0, which is often used in Silverlight or WPF).
Three . the benefits and functions of Task
- After a task is started, you can register the callback as a task continuation at any time.
- Coordinate multiple operations to respond to the Begin_ method by using the Continuewhenall and Continuewhenany methods or the WaitAll method or the WaitAny method.
- Encapsulates asynchronous I/O bindings and compute binding operations in the same Task object.
- Monitors the status of a Task object.
- Marshals the state of an operation to a Task object using TaskCompletionSource.
Four. use of Task
Please refer to this article
Five. using Task encapsulation for common asynchronous programming patterns
- Use the Task Encapsulation APM asynchronous Programming pattern. Async and await provided in c#5.0 make asynchronous programming easier. An await in MSDN explanation is that the "operator applies the execution of a task pending method to an async method until the task is awaited to complete." the task represents the work in progress. "It returns the result of task and task<tresult>. Below we will use a demo specific analysis:
Private async void Init () { //part code omitted var orgs = await _serviceclient.getallorganizationtaskasync (); } Call through WCF, take the distribution class, the same name. Public partial class serviceclient{public task<observablecollection<organization>> Getallorganizationtaskasync () { //task package APM return task<observablecollection<organization> . Factory.fromasync (this. Channel.begingetallorganization, this . Channel.endgetallorganization, null);} }
- Encapsulates the EPM asynchronous Pattern with a Task object. This pattern appears starting with. Net 2.0, and appears in a large number of Silverlight cases, which are characterized by the "Operation name Async" function and the "Operation name completed" event. The common operation is to use the LAMDA expression, or use + = "Operation name completed" (Tip: If you use the LAMDA expression, you cannot reclaim the resource, if you call more than one at the same time, the data will be messed up, it is recommended to use the + = "Operation name completed" If not used, In the action name completed event, again-= "Operation name completed").
Keywords: taskcompletionsource, simple to understand delegate to the task property, use task to manipulate.
var Source = new Taskcompletionsource<string> (), var webClient = new WebClient (); webclient.downloadstringcompleted + = (sender, args) = { if (args. Cancelled) { source. Setcanceled (); return; } if (args. Error! = null) { source. SetException (args. Error); return; } Source. Setresult (args. Result);}; Webclient.downloadstringasync (New Uri (Urltotest, urikind.absolute), null), source. Task.wait (); var result = source. Task.result;
This blog is from Http://www.cnblogs.com/luqixinhe/archive/2013/07/18/3197645.html, recorded in order to better study.