Bltoolkit-aspect-asyncaspect (i)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.