微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

WebService异步调用与委托有关

  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.Windows.Forms;
  9. namespace WindowsFormsApplication1
  10. {
  11.       public partial class Form1 : Form
  12.       {
  13.           public Form1()
  14.           {
  15.               InitializeComponent();
  16.           }
  17.           private void button1_Click(object sender, EventArgs e)
  18.           {
  19.               localhost.Service t = new WindowsFormsApplication1.localhost.Service();
  20.               MessageBox.Show(t.HelloWorld());
  21.               MessageBox.Show("Down!");
  22.           }
  23.           private void button2_Click(object sender, EventArgs e)
  24.           {
  25.               localhost.Service t = new WindowsFormsApplication1.localhost.Service();
  26.               t.HelloWorldCompleted += new WindowsFormsApplication1.localhost.HelloWorldCompletedEventHandler(t_HellowordCompleted);
  27.               t.HelloWorldAsync();
  28.               MessageBox.Show("Down!");
  29.           }
  30.           private void t_HellowordCompleted(object sender, localhost.HelloWorldCompletedEventArgs e)
  31.           {
  32.               MessageBox.Show(e.Result.ToString());
  33.           }
  34.           private void button3_Click(object sender, EventArgs e)
  35.           {
  36.               MessageBox.Show((new localhost.Service()).GetTime());
  37.           }
  38.       }
  39. }
  40. ---------------------webService----------------------------
  41. using System;
  42. using System.Linq;
  43. using System.Web;
  44. using System.Web.Services;
  45. using System.Web.Services.Protocols;
  46. using System.Xml.Linq;
  47. using System.Threading;
  48. [WebService(Namespace = "http://tempuri.org/")]
  49. [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  50. // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
  51. // [System.Web.Script.Services.ScriptService]
  52. public class Service : System.Web.Services.WebService
  53. {
  54.      public Service () {
  55.          //Uncomment the following line if using designed components 
  56.          //InitializeComponent(); 
  57.      }
  58.      [WebMethod]
  59.      public string HelloWorld() {
  60.          Thread.Sleep(3000);
  61.          return "Hello World";
  62.      }
  63.     ////缓存10秒
  64.      [WebMethod(false,System.EnterpriseServices.Transactionoption.NotSupported,10)]
  65.      public string GetTime()
  66.      {
  67.          return DateTime.Now.ToString();
  68.      }
  69.     
  70. }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐