Server-side asynchronous Web methods (ii)

Source: Internet
Author: User
Tags sleep stub thread
Web| Server | Asynchronous simple asynchronous Web method

To illustrate asynchronous Web methods, I start with a simple synchronous Web method called LengthyProcedure, whose code looks like this. Then we'll look at how to do the same task asynchronously. LengthyProcedure only takes a given number of milliseconds.

[WebService]

public class SyncWebService:System.Web.Services.WebService

{

[WebMethod]

public string LengthyProcedure (int milliseconds)

{

System.Threading.Thread.Sleep (milliseconds);

Return "Success";

}

}

Now we convert LengthyProcedure to asynchronous Web methods. We must create the BeginLengthyProcedure function and the EndLengthyProcedure function as described earlier. Keep in mind that our beginlengthyprocedure call needs to return a IAsyncResult interface. Here, I intend to use a delegate and the BeginInvoke method on the delegate to let our BeginLengthyProcedure call make an asynchronous method call. The callback function passed to BeginLengthyProcedure will be passed to the BeginInvoke method on the delegate, and the IAsyncResult returned from BeginInvoke will be returned by the BeginLengthyProcedure method.

When the delegate completes, the EndLengthyProcedure method is invoked. We will invoke the EndInvoke method on the delegate to pass in the IAsyncResult as input to the endlengthyprocedure call. The returned string will be the string returned from the Web method. Here's the code:

[WebService]

public class AsyncWebService:System.Web.Services.WebService

{

Public delegate String LengthyProcedureAsyncStub (

int milliseconds);



public string LengthyProcedure (int milliseconds)

{

System.Threading.Thread.Sleep (milliseconds);

Return "Success";

}



public class MyState

{

public object previousstate;

Public LengthyProcedureAsyncStub asyncstub;

}



[System.Web.Services.WebMethod]

Public IAsyncResult beginlengthyprocedure (int milliseconds,

AsyncCallback cb, Object s)

{

LengthyProcedureAsyncStub stub

= new LengthyProcedureAsyncStub (lengthyprocedure);

MyState ms = new MyState ();

Ms.previousstate = s;

ms.asyncstub = stub;

return stub. BeginInvoke (milliseconds, CB, MS);

}



[System.Web.Services.WebMethod]

public string EndLengthyProcedure (IAsyncResult call)

{

MyState ms = (mystate) call. asyncstate;

Return Ms.asyncStub.EndInvoke (call);

}

}


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.