Directory
asynchronous invocation
This aspect simplifies asynchronous operations. Note that Asyncaspect is not compatible with other properties if you apply them to an abstract aspect of asyncaspect. It will not work.
code example.
1. Referencing namespaces
using System.Threading; using bltoolkit.aspects; using Bltoolkit.reflection;
2. New Abstract class Asynctestobject
1 Public Abstract classAsynctestobject2 {3 /// <summary>4 ///We're going to call this method asynchronously5 /// </summary>6 /// <param name= "Intval" ></param>7 /// <param name= "Strval" ></param>8 /// <returns></returns>9 Public intTest (intIntval,stringstrval)Ten { OneThread.Sleep ( $); A returnintval; - } - the - //The start Async method should have the same parameters as the method that needs to be called. and return IAsyncResult - //can provide two additional parameters AsyncCallback and state object - //the ' Begin ' prefix is part of the Convention + [Async] - Public AbstractIAsyncResult Begintest (intIntval,stringstrval); + A [Async] at Public AbstractIAsyncResult Begintest (intIntval,stringStrval, AsyncCallback callback); - - [Async] - Public AbstractIAsyncResult Begintest (intIntval,stringStrval, AsyncCallback callback,ObjectState ); - - //the end async method should use IAsyncResult as the incoming parameter and the return value should match the method that needs to be called in //the ' End ' prefix is part of the Convention - [Async] to Public Abstract intendtest (IAsyncResult asyncResult); + - //Begin/end prefixes are not required. You can use any name the //If you provide a target method name as a parameter, pass in the Async property. *[Async ("Test")] $ Public AbstractIAsyncResult Anyname (intIntval,stringStrval, AsyncCallback callback,ObjectState );Panax Notoginseng - //here we provide the parameter list and the method name the[Async ("Test",typeof(int),typeof(string))] + Public Abstract intanyname (IAsyncResult asyncResult); A}
3. Testing
Static voidMain (string[] args) { //Test a//asynctestobject o = typeaccessor<asynctestobject>. CreateInstance (); //IAsyncResult ar = o.begintest (1, "10"); //Console.WriteLine (O.endtest (AR)); //Test Two//asynctestobject o = typeaccessor<asynctestobject>. CreateInstance (); //IAsyncResult ar = o.begintest (2, null,callback,o);asynctestobject o= typeaccessor<asynctestobject>. CreateInstance (); varAsyncResult = O.anyname (1,NULL,NULL,NULL); Console.WriteLine (O.anyname (AsyncResult)); Console.readkey (); } /// <summary> ///callback function/// </summary> /// <param name= "ar" ></param> Private Static voidCallBack (IAsyncResult ar) {Console.WriteLine ("Callback"); Asynctestobject o=(asynctestobject) ar. asyncstate; O.endtest (AR); }
Bltoolkit-aspect-asyncaspect (i)