zoukankan      html  css  js  c++  java
  • 如何:使用回调方法实现异步 Web 服务客户端

    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){
                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 accessible 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] + ", ");
                }
          }
    }

  • 相关阅读:
    hadoop集群的搭建
    EclipseAndroid打包签名发布安装失败闪退运行不了
    [目录]C#学习笔记
    [目录]搭建一个简单的WebGIS应用程序
    实现DataTables搜索框查询结果高亮显示
    解决将Excel表导入到SQL Server数据库时出现Text was truncated or one or more characters had no match in the target code错误
    将展示内容(div、iframe)放在Expand控件中
    [C#学习笔记1]用csc.exe和记事本写一个C#应用程序
    选中FeatureLayer元素并高亮显示
    在地图中调用显示FeatureLayer并进行render、popupTemplate、添加图例等相关内容的设置
  • 原文地址:https://www.cnblogs.com/wzyexf/p/389750.html
Copyright © 2011-2022 走看看