[C#] [WebMethod] public IAsyncResult BeginGetAuthorRoyalties(String Author, AsyncCallback callback, object asyncState) [Visual Basic] <WebMethod()> _ Public Function BeginGetAuthorRoyalties(ByVal Author As String, _ ByVal callback As AsyncCallback, ByVal asyncState As Object) _ As IAsyncResult |
[C#] [WebMethod] public AuthorRoyalties EndGetAuthorRoyalties(IAsyncResult asyncResult) [Visual Basic] <WebMethod()> _ Public Function EndGetAuthorRoyalties(ByVal asyncResult As _ IAsyncResult) As AuthorRoyalties |
[C#] using System; using System.Web.Services; [WebService(Namespace="http://www.contoso.com/")] public class MyService : WebService { public RemoteService remoteService; public MyService() { // Create a new instance of proxy class for // the XML Web service to be called. remoteService = new RemoteService(); } // Define the Begin method. [WebMethod] public IAsyncResult BeginGetAuthorRoyalties(String Author,AsyncCallback callback, object asyncState) { // Begin asynchronous communictation with a different XML Web // service. return remoteService.BeginReturnedStronglyTypedDS(Author, callback,asyncState); } // Define the End method. [WebMethod] public AuthorRoyalties EndGetAuthorRoyalties(IAsyncResultasyncResult) { // Return the asynchronous result from the other XML Web service. return remoteService.EndReturnedStronglyTypedDS(asyncResult); } } [Visual Basic] Imports System.Web.Services <WebService(Namespace:="http://www.contoso.com/")> _ Public Class MyService Inherits WebService Public remoteService As RemoteService Public Sub New() MyBase.New() ' Create a new instance of proxy class for ' the XML Web service to be called. remoteService = New RemoteService() End Sub ' Define the Begin method. <WebMethod()> _ Public Function BeginGetAuthorRoyalties(ByVal Author As String, _ ByVal callback As AsyncCallback, ByVal asyncState As Object) _ As IAsyncResult ' Begin asynchronous communictation with a different XML Web ' service. Return remoteService.BeginReturnedStronglyTypedDS(Author, _ callback, asyncState) End Function ' Define the End method. <WebMethod()> _ Public Function EndGetAuthorRoyalties(ByVal asyncResult As _ IAsyncResult) As AuthorRoyalties ' Return the asynchronous result from the other XML Web service. Return remoteService.EndReturnedStronglyTypedDS(asyncResult) End Function End Class |
[C#] using System.Web.Services; using System.Data; using System; // This imports the proxy class for the XML Web services // that the sample communicates with. using AsyncWS.localhost; namespace AsyncWS { [WebService(Namespace="http://www.contoso.com/")] public class MyService : System.Web.Services.WebService { public RemoteService remoteService; public MyService() { remo teService = new RemoteService(); } [WebMethod] public IAsyncResult BeginGetAuthorRoyalties(String Author, AsyncCallback callback, Object asyncState) { // Saves the current state for the call that gets the author's // royalties. AsyncStateChain state = new AsyncStateChain(); state.originalState = asyncState; state.Author = Author; state.originalCallback = callback; // Creates an intermediary callback. AsyncCallback chainedCallback = new AsyncCallback(AuthorRoyaltiesCallback); return remoteService.BeginGetAuthors(chainedCallback,state); } // Intermediate method to handle chaining the // asynchronous calls. public void AuthorRoyaltiesCallback(IAsyncResult ar) { AsyncStateChain state = (AsyncStateChain)ar.AsyncState; RemoteService rs = new RemoteService(); // Gets the result from the call to GetAuthors. Authors allAuthors = rs.EndGetAuthors(ar); Boolean found = false; // Verifies that the requested author is valid. int i = 0; DataRow row; while (i < allAuthors.authors.Rows.Count && !found) { row = allAuthors.authors.Rows[i]; if (row["au_lname"].ToString() == state.Author) { found = true; } i++; } if (found) { AsyncCallback cb = state.originalCallback; // Calls the second XML Web service, because the author is // valid. rs.BeginReturnedStronglyTypedDS(state.Author,cb,state); } else { // Cannot throw the exception in this function or the XML Web // service will hang. So, set the state argument to the // exception and let the End method of the chained XML Web // service check for it. ArgumentException ex = new ArgumentException( "Author does not exist.","Author"); AsyncCallback cb = state.originalCallback; // Call the second XML Web service, setting the state to an // exception. rs.BeginReturnedStronglyTypedDS(state.Author,cb,ex); } } [WebMethod] public AuthorRoyalties EndGetAuthorRoyalties(IAsyncResult asyncResult) { // Check whehter the first XML Web service threw an exception. if (asyncResult.AsyncState is ArgumentException) throw (ArgumentException) asyncResult.AsyncState; else return remoteService.EndReturnedStronglyTypedDS(asyncResult); } } // Class to wrap the callback and state for the intermediate // asynchronous operation. public class AsyncStateChain { public AsyncCallback originalCallback; public Object originalState; public String Author; } } [Visual Basic] Imports System.Web.Services Imports System.Data Imports System ' This imports the proxy class for the XML Web services ' that the sample communicates with. Imports AsyncWS_VB.localhost Namespace AsyncWs <WebService(Namespace:="http://www.contoso.com/")> _ Public Class MyService Inherits WebService Public remoteService As remoteService Public Sub New() MyBase.New() remoteService = New localhost.RemoteService() End Sub ' Defines the Begin method. <WebMethod()> _ Public Function BeginGetAuthorRoyalties(ByVal Author As String, _ ByVal callback As AsyncCallback, ByVal asyncState As Object) _ As IAsyncResult ' Saves the current state for the call that gets the author's ' royalties. Dim state As AsyncStateChain = New AsyncStateChain() state.originalState = asyncState state.Author = Author state.originalCallback = callback ' Creates an intermediary callback. Dim chainedCallback As AsyncCallback = New AsyncCallback( _ AddressOf AuthorRoyaltiesCallback) ' Begin asynchronous communictation with a different XML Web ' service. Return remoteService.BeginGetAuthors(chainedCallback, state) End Function ' Intermediate method to handle chaining the asynchronous calls. Public Sub AuthorRoyaltiesCallback(ByVal ar As IAsyncResult) Dim state As AsyncStateChain = CType(ar.AsyncState, _ AsyncStateChain) Dim rs As RemoteService = New RemoteService() ' Gets the result from the call to GetAuthors. Dim allAuthors As Authors = rs.EndGetAuthors(ar) Dim found As Boolean = False ' Verifies that the requested author is valid. Dim i As Integer = 0 Dim row As DataRow While (i < allAuthors.authors.Rows.Count And (Not found)) row = allAuthors.authors.Rows(i) If (row("au_lname").ToString() = state.Author) Then found = True End If i = i + 1 End While If (found) Then Dim cb As AsyncCallback = state.originalCallback ' Calls the second XML Web service, because the author is ' valid. rs.BeginReturnedStronglyTypedDS(state.Author, cb, state) Else ' Cannot throw the exception in this function or the XML Web ' service will hang. So, set the state argument to the ' exception and let the End method of the chained XML Web ' service check for it. Dim ex As ArgumentException = New ArgumentException( "Author does not exist.", "Author") Dim cb As AsyncCallback = state.originalCallback ' Call the second XML Web service, setting the state to an ' exception. rs.BeginReturnedStronglyTypedDS(state.Author, cb, ex) End If End Sub ' Define the End method. <WebMethod()> _ Public Function EndGetAuthorRoyalties(ByVal asyncResult As _ IAsyncResult) As localhost.AuthorRoyalties ' Return the asynchronous result from the other XML Web service. Return remoteService.EndReturnedStronglyTypedDS(asyncResult) End Function End Class ' Class to wrap the callback and state for the intermediate asynchronous ' operation. Public Class AsyncStateChain Public originalCallback As AsyncCallback Public originalState As Object Public Author As String End Class End Namespace |
[C#] <%@ WebService Language="C#" Class="PrimeFactorizer" %> using System; using System.Collections; using System.Web.Services; class PrimeFactorizer { [WebMethod] public long[] Factorize(long factorizableNum){ ArrayList outList = new ArrayList(); long i = 0; int j; try{ long Check = factorizableNum; //Go through every possible integer //factor between 2 and factorizableNum / 2. //Thus, for 21, check between 2 and 10. for (i = 2; i < (factorizableNum / 2); i++){ while(Check % i == 0){ outList.Add(i); Check = (Check/i); } } //Double-check to see how many prime factors have been added. //If none, add 1 and the number. j = outList.Count; if (j == 0) { outList.Add(1); outList.Add(factorizableNum); } j = outList.Count; //Return the results and //create an array to hold them. long[] primeFactor = new long[j]; for (j = 0; j < outList.Count; j++){ //Pass the values one by one, making sure //to convert them to type ulong. primeFactor[j] = Convert.ToInt64(outList[j]); } return primeFactor; } catch (Exception) { return null; } } } [Visual Basic] <%@ WebService Class="PrimeFactorizer" Language="VB" %> Imports System Imports System.Collections Imports System.Web.Services Public Class PrimeFactorizer <WebMethod> _ Public Function Factorize(factorizableNum As Long) As Long() Dim outList As New ArrayList() Dim i As Long = 0 Dim j As Integer Try Dim Check As Long = factorizableNum 'Go through every possible integer 'factor between 2 and factorizableNum / 2. 'Thus, for 21, check between 2 and 10. For i = 2 To CLng(factorizableNum / 2) - 1 While Check Mod i = 0 outList.Add(i) Check = CLng(Check / i) End While Next i 'Double-check to see how many prime factors have been added. 'If none, add 1 and the number. j = outList.Count If j = 0 Then outList.Add(1) outList.Add(factorizableNum) End If j = outList.Count 'Return the results and 'create an array to hold them. Dim primeFactor(j - 1) As Long For j = 0 To outList.Count - 1 'Pass the values one by one, making sure 'to convert them to type ulong. primeFactor(j) = CLng(outList(j)) Next j Return primeFactor Catch Return Nothing End Try End Function End Class |
public class PrimeFactorizer : System.Web.Services.Protocols.SoapHttpClientProtocol { public long[] Factorize(long factorizableNum) { object[] results = this.Invoke("Factorize", new object[] { factorizableNum}); return ((long[])(results[0])); } public System.IAsyncResult BeginFactorize(long factorizableNum, System.AsyncCallback callback, object asyncState) { return this.BeginInvoke("Factorize", new object[] { factorizableNum}, callback, asyncState); } public long[] EndFactorize(System.IAsyncResult asyncResult) { object[] results = this.EndInvoke(asyncResult); return ((long[])(results[0])); } } |
[C#] using System; using System.Runtime.Remoting.Messaging; using MyFactorize; class TestCallback { public static void Main(){ long factorizableNum = 12345; PrimeFactorizer pf = new PrimeFactorizer(); //Instantiate an AsyncCallback delegate to use as a parameter //in the BeginFactorize method. AsyncCallback cb = new AsyncCallback(TestCallback.FactorizeCallback); // Begin the Async call to Factorize, passing in our // AsyncCalback delegate and a reference // to our instance of PrimeFactorizer. IAsyncResult ar = pf.BeginFactorize(factorizableNum, cb, pf); // Keep track of the time it takes to complete the async call // as the call proceeds. int start = DateTime.Now.Second; int currentSecond = start; while (ar.IsCompleted == false){ if (currentSecond < DateTime.Now.Second) { currentSecond = DateTime.Now.Second; Console.WriteLine("Seconds Elapsed..." + (currentSecond - start).ToString() ); } } // Once the call has completed, you need a method to ensure the // thread executing this Main function // doesn't complete prior to the call-back function completing. Console.Write("Press Enter to quit"); int quitchar = Console.Read(); } // Set up a call-back function that is invoked by the proxy class // when the asynchronous operation completes. public static void FactorizeCallback(IAsyncResult ar) { // You passed in our instance of PrimeFactorizer in the third // parameter to BeginFactorize, which is aclearcase/" target="_blank" >ccessible in the // AsyncState property. PrimeFactorizer pf = (PrimeFactorizer) ar.AsyncState; long[] results; // Get the completed results. results = pf.EndFactorize(ar); //Output the results. Console.Write("12345 factors into: "); int j; for (j = 0; j<results.Length;j++){ if (j == results.Length - 1) Console.WriteLine(results[j]); else Console.Write(results[j] + ", "); } } } [Visual Basic] Imports System Imports System.Runtime.Remoting.Messaging Imports MyFactorize Public Class TestCallback Public Shared Sub Main() Dim factorizableNum As Long = 12345 Dim pf As PrimeFactorizer = new PrimeFactorizer() 'Instantiate an AsyncCallback delegate to use as a parameter ' in the BeginFactorize method. Dim cb as AsyncCallback cb = new AsyncCallback(AddressOf TestCallback.FactorizeCallback) ' Begin the Async call to Factorize, passing in the ' AsyncCallback delegate and a reference to our instance ' of PrimeFactorizer. Dim ar As IAsyncResult = pf.BeginFactorize(factorizableNum, cb, pf) ' Keep track of the time it takes to complete the async call as ' the call proceeds. Dim start As Integer = DateTime.Now.Second Dim currentSecond As Integer = start Do while (ar.IsCompleted = false) If (currentSecond < DateTime.Now.Second) Then currentSecond = DateTime.Now.Second Console.WriteLine("Seconds Elapsed..." + (currentSecond - start).ToString() ) End If Loop ' Once the call has completed, you need a method to ensure the ' thread executing this Main function ' doesn't complete prior to the callback function completing. Console.Write("Press Enter to quit") Dim quitchar As Integer = Console.Read() End Sub ' Set up the call-back function that is invoked by the proxy ' class when the asynchronous operation completes. Public Shared Sub FactorizeCallback(ar As IAsyncResult) ' You passed in the instance of PrimeFactorizer in the third ' parameter to BeginFactorize, which is accessible in the ' AsyncState property. Dim pf As PrimeFactorizer = ar.AsyncState Dim results() as Long ' Get the completed results. results = pf.EndFactorize(ar) 'Output the results. Console.Write("12345 factors into: ") Dim j as Integer For j = 0 To results.Length - 1 If j = (results.Length - 1) Then Console.WriteLine(results(j) ) Else Console.Write(results(j).ToString + ", ") End If Next j End Sub End Class |
[C#] // -----------------------------------------------------------------------// Async Variation 2. // Asynchronously invoke the Factorize method, //without specifying a call back. using System; using System.Runtime.Remoting.Messaging; // MyFactorize, is the name of the namespace in which the proxy class is // a member of for this sample. using MyFactorize; class TestCallback { public static void Main(){ long factorizableNum = 12345; PrimeFactorizer pf = new PrimeFactorizer(); // Begin the Async call to Factorize. IAsyncResult ar = pf.BeginFactorize(factorizableNum, null, null); // Wait for the asynchronous operation to complete. ar.AsyncWaitHandle.WaitOne(); // Get the completed results. long[] results; results = pf.EndFactorize(ar); //Output the results. Console.Write("12345 factors into: "); int j; for (j = 0; j<results.Length;j++){ if (j == results.Length - 1) Console.WriteLine(results[j]); else Console.Write(results[j] + ", "); } } } [Visual Basic] Imports System Imports System.Runtime.Remoting.Messaging Imports MyFactorize ' Proxy class namespace Public Class TestCallback Public Shared Sub Main() Dim factorizableNum As Long = 12345 Dim pf As PrimeFactorizer = new PrimeFactorizer() ' Begin the Async call to Factorize. Dim ar As IAsyncResult = pf.BeginFactorize(factorizableNum, Nothing, Nothing) ' Wait for the asynchronous operation to complete. ar.AsyncWaitHandle.WaitOne() ' Get the completed results. Dim results() as Long results = pf.EndFactorize(ar) 'Output the results. Console.Write("12345 factors into: ") Dim j as Integer For j = 0 To results.Length - 1 If j = (results.Length - 1) Then Console.WriteLine(results(j) ) Else Console.Write(results(j).ToString + ", ") End If Next j End Sub End Class |