一个简单的webservice的demo(下)winform异步调用webservice

摘要:
编写对webservice演示的winform异步调用仍然很简单。一个简单的Web服务演示,一个简单模拟服务,一个简易Web服务演示(中间)_前端页面调用当winform同步调用服务时,因为调用服务不能像C/S那样快,所以winform UI进程一直在等待服务的返回结果,并且无法响应用户事件。首先,准备一个用于模拟的web服务,如下:1使用System;2使用System.Collections.Generic;3使用System.Linq;4使用System.Threading;5使用System.Web;6使用System.Web.Services;78namespaceWFasy9{10////<summary>11////AsyWeb的摘要描述12////</summary>13[WebService]14[WebServiceBinding]15[System.ComponentModel.ToolboxItem]16//允许使用ASP。NETAJAX从脚本调用此Web服务,请取消注释以下行。这是目前的下一章,未来将缓慢添加一系列后续的web服务章节。

  绕了一大圈,又开始接触winform的项目来了,虽然很小吧。写一个winform的异步调用webservice的demo,还是简单的。

  一个简单的Webservice的demo,简单模拟服务

  一个简单的Webservice的demo(中)_前端页面调用

  当winform同步调用服务时,由于调用服务不能像C/S那样快,winform的UI进程一直在等待服务的返回结果,就无法响应用户事件。为了解决这种问题,我们用异步调用。

  首先,先准备一个模拟用的webservice,如下:

  

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Threading;
 5 using System.Web;
 6 using System.Web.Services;
 7 
 8 namespace WFasy
 9 {
10     /// <summary>
11     /// AsyWeb 的摘要说明
12     /// </summary>
13     [WebService(Namespace = "http://tempuri.org/")]
14     [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
15     [System.ComponentModel.ToolboxItem(false)]
16     // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。 
17     // [System.Web.Script.Services.ScriptService]
18     public class AsyWeb : System.Web.Services.WebService
19     {
20 
21         [WebMethod(Description="模拟服务等待,返回输入字符的前三位")]
22         public string AsynchronousCall(string code)
23         {
24             // 线程sleep5秒,模拟服务请求等待。
25             Thread.Sleep(5000);
26             if (code.Length >= 3)
27                 return code.Substring(0, 3);
28             return "***";
29         }
30     }
31 }

  现在写winform客户端,引用服务:

  一个简单的webservice的demo(下)winform异步调用webservice第1张

  点击高级,选择生成异步操作(应注意)

  一个简单的webservice的demo(下)winform异步调用webservice第2张

  客户端代码:

  

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Threading.Tasks;
 9 using System.Windows.Forms;
10 
11 namespace WFAsyC
12 {
13     public partial class form : Form
14     {
15         public form()
16         {
17             InitializeComponent();
18         }
19 
20         private void btnSure_Click(object sender, EventArgs e)
21         {
22             string inString = txtIn.Text;
23             string outString = txtOut.Text;
24 
25             WS.AsyWebSoapClient ws = new WS.AsyWebSoapClient();
26 
27             // 在调用服务前,应注册调用完成事件,即“回调方法”
28             // 一定要在异步调用前注册
29             ws.AsynchronousCallCompleted += ws_AsynchronousCallCompleted;
30 
31             // 我们之前用这个方法调用服务
32             // ws.AsynchronousCall(txtIn.Text);
33             // 异步调用时用Asyn
34             ws.AsynchronousCallAsync(txtIn.Text);
35         }
36 
37         /// <summary>
38         /// 调用成功后触发此事件
39         /// </summary>
40         /// <param name="sender">object</param>
41         /// <param name="e">EventArgs</param>
42         void ws_AsynchronousCallCompleted(object sender, WS.AsynchronousCallCompletedEventArgs e)
43         {
44             if (e.Error == null)
45             {
46                 // 若调用服务没有异常
47                 txtOut.Text = e.Result;
48             }
49             else
50             {
51                 txtOut.Text = "服务端出现异常!";
52             }
53         }
54     }
55 }

  最近很忙了,以后坚持写博客,写下一些最简单的demo,记录自己成长过程。这里暂且为下篇了,未来慢慢添加一系列webservice的后续篇章。

免责声明:文章转载自《一个简单的webservice的demo(下)winform异步调用webservice》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇17.可选链STM32——timer下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

c# winform 智能模糊匹配 输入框

using System;using System.Collections.Generic;using System.ComponentModel;using System.Drawing;using System.Data;using System.Linq;using System.Text;using System.Windows.Forms;usi...

winform 显示时钟效果

1、winform里拖拽控件 timer 2、为timer控件 绑定事件 3、程序代码如下 1 usingSystem; 2 usingSystem.Drawing; 3 usingSystem.Windows.Forms; 4 usingSystem.Drawing.Drawing2D; 5 6 namespaceVH_CriticalRepor...

教你七招提高.NET网站性能

一、减少往返行程(Reduce Round Trips) 使用下面的方法可以减少Web服务器和Browser之间的往返行程: 1、为Browser启用缓存 如果呈现的内容是静态的或变化周期较长,应启用Browser缓存,避免发出冗余的http请求。 2、缓冲页面输出 如果可能,则尽量缓冲页面输出,处理结束后再一次传送到客户端,这可以避免频繁传递小块...

winform 中 MessageBox 用法大全

(转自:http://blog.csdn.net/xuenzhen123/article/details/4808005)  MessageBox.Show()共有21中重载方法。现将其常见用法总结如下:   1.MessageBox.Show(“Hello~~~~”); 最简单的,只显示提示信息。 2.MessageBox.Show(“There ar...

WinForm 进程、线程

一、进程 进程是一个具有独立功能的程序关于某个数据集合的一次运行活动。 它可以申请和拥有系统资源,是一个动态的概念,是一个活动的实体。 Process 类,用来操作进程。 命名空间:using System.Diagnostics; Process.Start("calc");   //打开计算器Process.Start("mspaint");   /...

手把手图文教程让你C#开发的winform程序打包发布应用和卸载程序

1:新建安装部署项目 打开VS,点击新建项目,选择:其他项目类型->安装与部署->安装向导(安装项目也一样),然后点击确定.(详细见下图) 此主题相关图片如下: 2:安装向导关闭后打开安装向导,点击下一步,或者直接点击完成. 3:开始制作 安装向导完成后即可进入项目文件夹: 双击"应用程序文件夹"在右边的空白处右击,选择添加->文件,将你...