讀書筆記:《亮劍 .Net》——實現非同步呼叫Web Services

來源:互聯網
上載者:User

普通在調用WebService的時候,程式會等待Web Service執行成功之後才執行接下來的邏輯,這樣有可能會造成用戶端UI線程的阻塞假死現象,這個時候可以使用非同步呼叫Web Service的辦法解決這個問題。

第一種方法:

通過利用Backgroundworker對象實現,Backgroundworker類允許你在單獨的專用線程上運行操作。

 

private void button1_Click(object sender, EventArgs e)
        {
            BackgroundWorker backgroundworker = new BackgroundWorker();
            // 註冊具體非同步處理的方法
            backgroundworker.DoWork += new DoWorkEventHandler(back_DoWork);
            // 註冊調用完成後的回調方法
            backgroundworker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(back_RunWorkerCompleted);
            // 這裡開始非同步呼叫
            backgroundworker.RunWorkerAsync();
            //調用服務的同時用戶端處理並不停止
            ChangeProcessBar();
        }
        //完成事件
        void back_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Error != null)
                throw e.Error;
            progressBar1.Value = progressBar1.Maximum; //調用完成了,把用戶端進度條填充滿
            string price = e.Result.ToString();   //擷取處理結果
            MessageBox.Show("調用完成。價格是:" + price); //顯示從伺服器擷取的結果值
        }
        //調用方法
        void back_DoWork(object sender, DoWorkEventArgs e)
        {
            //Web Service代理類
            ProductService.LTPService service = new ProductService.LTPService();
            // 調用Web方法GetClass1,結果賦值給DoWorkEventArgs的Result對象
            e.Result = service.GetProductPrice("001");
        }

 

第二種方法:

調用Web Service的WebMethod中的Async方法實現。

當添加完Web Service的引用以後,會在本地生產代理類,其中,會有一個和原Web Service方法名字相同而尾碼是Async的方法

 

private void button2_Click(object sender, EventArgs e)
        {
            ProductService.LTPService service = new ProductService.LTPService();//Web Service代理類
            //這裡開始非同步呼叫
            service.GetProductPriceAsync("001");
            // 註冊調用完成後的回調方法
            service.GetProductPriceCompleted += new ProductService.GetProductPriceCompletedEventHandler(GetProductPriceCompleted);
            //調用同時用戶端處理不停止
            ChangeProcessBar();
        }
        //完成事件
        void GetProductPriceCompleted(object sender, ProductService.GetProductPriceCompletedEventArgs e)
        {
            if (e.Error != null)
                throw e.Error;
            progressBar1.Value = progressBar1.Maximum; //調用完成了,把用戶端進度條填充滿
            string price = e.Result.ToString(); //擷取處理結果
            MessageBox.Show("調用完成。價格是:" + price); //顯示從伺服器擷取的結果值
        }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.