• 异步调用 WebService 及 同步模式下通过Session以共享计数


    1.使用等待方法实现异步
    2.使用回调方法实现异步
    3.同步模式下 通过Session以共享计数
    ==============================

    1.使用等待方法实现异步
    ----------------------
    [WebMethod]
    public string HelloWorld()
    {
        System.Threading.Thread.Sleep(2000);
        return "Hello World";
    }

    加入引用

    相关调用

    protected void Button1_Click(object sender, EventArgs e)
    {
        localhost.WebServiceA wsA = new localhost.WebServiceA();
        IAsyncResult iar = wsA.BeginHelloWorld(null,null);
        iar.AsyncWaitHandle.WaitOne();
        string strReturn = wsA.EndHelloWorld(iar);
        this.TextBox1.Text = strReturn;
    }

    2.使用回调方法实现异步
    ----------------------
    [WebMethod]
    public string HelloWorld()
    {
        System.Threading.Thread.Sleep(2000);
        return "Hello World";
    }

    加入引用

    相关调用
    protected void Button1_Click(object sender, EventArgs e)
    {
        localhost.WebServiceA wsA = new localhost.WebServiceA();
        System.AsyncCallback acb = new AsyncCallback(GetReturnValueCallback);
        IAsyncResult iar = wsA.BeginHelloWorld(acb, wsA);
        while (!iar.IsCompleted)
        {
           
        }   
    }

    private void GetReturnValueCallback(IAsyncResult ar)
    {
        localhost.WebServiceA wsA = (localhost.WebServiceA)ar.AsyncState;
        string strReturn = wsA.EndHelloWorld(ar);
        this.TextBox1.Text = strReturn;
    }

    3.同步模式下 通过Session以共享计数
    ------------------------------
    [WebMethod]
    public string HelloWorld()
    {
        System.Threading.Thread.Sleep(2000);
        int intCount = GetCount;
        return "第 "+intCount + " 次 Hello World";
    }

    private int GetCount
    {
        get
        {
            if (Session["MyCount"] == null)
            {
                Session["MyCount"] = 1;
                return 1;
            }
            else
            {
                int intNowCount = Convert.ToInt32(Session["MyCount"].ToString().Trim());
                intNowCount++;
                Session["MyCount"] = intNowCount;
                return intNowCount;
            }
        }
    }

  • 相关阅读:
    模块的种类和导入方法
    小知识点补充
    9.17模拟赛2.0
    hdu2181 哈密顿绕行世界问题
    9.17模拟赛
    9.15模拟赛
    P1084 疫情控制
    9.14模拟赛
    【bzoj1232】[Usaco2008Nov]安慰奶牛cheer
    P3128 [USACO15DEC]最大流Max Flow
  • 原文地址:https://www.cnblogs.com/freeliver54/p/762347.html
Copyright © 2020-2023  润新知