技术积累

明日复明日,明日何其多,我生待明日,万事成蹉跎。
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Web service 五种调用方式

Posted on 2005-08-18 22:50  追风逐云.NET  阅读(390)  评论(0编辑  收藏  举报
Web Service
  [WebMethod]
  public int DoSomeWork(int TimeSpan)
  {
   System.Threading.Thread.Sleep(TimeSpan);
   Random rand=new Random();
   return rand.Next(100,1000);
  }

****************************************************************
using System.Windows.Forms;
using System.Data;

namespace WindowsTest
{
 /// <summary>
 /// Form1 的摘要说明。
 /// </summary>
 public class Form1 : System.Windows.Forms.Form
 {
  private System.Windows.Forms.Button button1;
  private System.Windows.Forms.TextBox textBox1;
  private System.Windows.Forms.Button button2;
  private System.Windows.Forms.Button button3;
  private System.Windows.Forms.Button button4;
  private System.Windows.Forms.Button button5;
  /// <summary>
  /// 必需的设计器变量。
  /// </summary>
  private System.ComponentModel.Container components = null;

  public Form1()
  {
   //
   // Windows 窗体设计器支持所必需的
   //
   InitializeComponent();

   //
   // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
   //
  }

  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows 窗体设计器生成的代码
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {
   this.button1 = new System.Windows.Forms.Button();
   this.textBox1 = new System.Windows.Forms.TextBox();
   this.button2 = new System.Windows.Forms.Button();
   this.button3 = new System.Windows.Forms.Button();
   this.button4 = new System.Windows.Forms.Button();
   this.button5 = new System.Windows.Forms.Button();
   this.SuspendLayout();
   //
   // button1
   //
   this.button1.Location = new System.Drawing.Point(128, 64);
   this.button1.Name = "button1";
   this.button1.Size = new System.Drawing.Size(96, 32);
   this.button1.TabIndex = 0;
   this.button1.Text = "同  步";
   this.button1.Click += new System.EventHandler(this.button1_Click);
   //
   // textBox1
   //
   this.textBox1.Location = new System.Drawing.Point(104, 16);
   this.textBox1.Name = "textBox1";
   this.textBox1.Size = new System.Drawing.Size(96, 21);
   this.textBox1.TabIndex = 1;
   this.textBox1.Text = "1000";
   //
   // button2
   //
   this.button2.Location = new System.Drawing.Point(128, 120);
   this.button2.Name = "button2";
   this.button2.Size = new System.Drawing.Size(96, 32);
   this.button2.TabIndex = 0;
   this.button2.Text = "异 步";
   this.button2.Click += new System.EventHandler(this.button2_Click);
   //
   // button3
   //
   this.button3.Location = new System.Drawing.Point(128, 168);
   this.button3.Name = "button3";
   this.button3.Size = new System.Drawing.Size(96, 32);
   this.button3.TabIndex = 0;
   this.button3.Text = "线 程";
   this.button3.Click += new System.EventHandler(this.button3_Click);
   //
   // button4
   //
   this.button4.Location = new System.Drawing.Point(248, 120);
   this.button4.Name = "button4";
   this.button4.Size = new System.Drawing.Size(96, 32);
   this.button4.TabIndex = 0;
   this.button4.Text = "等待异步";
   this.button4.Click += new System.EventHandler(this.button4_Click);
   //
   // button5
   //
   this.button5.Location = new System.Drawing.Point(16, 120);
   this.button5.Name = "button5";
   this.button5.Size = new System.Drawing.Size(96, 32);
   this.button5.TabIndex = 0;
   this.button5.Text = "另一种异步";
   this.button5.Click += new System.EventHandler(this.button5_Click);
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(392, 222);
   this.Controls.Add(this.textBox1);
   this.Controls.Add(this.button1);
   this.Controls.Add(this.button2);
   this.Controls.Add(this.button3);
   this.Controls.Add(this.button4);
   this.Controls.Add(this.button5);
   this.Name = "Form1";
   this.Text = "改进后异步";
   this.Load += new System.EventHandler(this.Form1_Load);
   this.ResumeLayout(false);

  }
  #endregion

  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.Run(new Form1());
  }

  private void Form1_Load(object sender, System.EventArgs e)
  {
   
  }
//*********************************同步
  private void button1_Click(object sender, System.EventArgs e)
  {
   index.index ind=new WindowsTest.index.index();
   MessageBox.Show(ind.DoSomeWork(int.Parse(textBox1.Text)).ToString(),"同步调用",MessageBoxButtons.OK,MessageBoxIcon.Question);  

  }
//*******************************异步
  private delegate int DelegateFun(int Time);

  private void button2_Click(object sender, System.EventArgs e)
  {
   index.index call=new WindowsTest.index.index();
   System.AsyncCallback AsyncCallback=new AsyncCallback(this.MyCallBack);//异步操作完成时调用方法
   DelegateFun AsyncDelegate=new DelegateFun(call.DoSomeWork);
   System.IAsyncResult AsyncResult;//异步操作作态
   AsyncResult=AsyncDelegate.BeginInvoke (int.Parse(textBox1.Text),AsyncCallback,null);//异步开始调用

  }
  private void MyCallBack(System.IAsyncResult ar)//ar异步操作状态
  {
   DelegateFun AsyncDelegate;
   System.Runtime.Remoting.Messaging.AsyncResult AsyncResult;//异步操作结果
   int ReturnValue;
   AsyncResult=(System.Runtime.Remoting.Messaging.AsyncResult)ar;
   AsyncDelegate=(DelegateFun)AsyncResult.AsyncDelegate;//获取委托对象
   ReturnValue=AsyncDelegate.EndInvoke(AsyncResult);//实际调用
   MessageBox.Show(ReturnValue.ToString());
  }
//***************************************改进型异步
  private index.index callTo=new WindowsTest.index.index();

  private void button5_Click(object sender, System.EventArgs e)
  {
   System.AsyncCallback AsyncCallback=new AsyncCallback(this.MyCallBack1);//异步操作完成时调用方法
   callTo.BeginDoSomeWork(int.Parse(textBox1.Text),AsyncCallback,null);   
  }
  private void MyCallBack1(System.IAsyncResult ar)//ar异步操作状态
  {
   int ReturnValue;
   ReturnValue=callTo.EndDoSomeWork(ar);//实际调用
   MessageBox.Show("改进型异步调用---Result:"+ReturnValue.ToString());
  }
//***********************************等待异步


  private void button4_Click(object sender, System.EventArgs e)
  {
   index.index call=new WindowsTest.index.index();
   System.IAsyncResult ar=call.BeginDoSomeWork(int.Parse(textBox1.Text),null,null);
   ar.AsyncWaitHandle.WaitOne();
   MessageBox.Show("等待型异步调用---Result:"+call.EndDoSomeWork(ar).ToString());

  }
//***********************************线程

  private void button3_Click(object sender, System.EventArgs e)
  {
   System.Threading.Thread thread=new System.Threading.Thread(new System.Threading.ThreadStart(this.ThreadCall));
   thread.Start();  
  }
  private void ThreadCall()
  {
   index.index ind=new WindowsTest.index.index();
   MessageBox.Show(ind.DoSomeWork(int.Parse(textBox1.Text)).ToString(),"线程同步调用",MessageBoxButtons.OK,MessageBoxIcon.Question);  
  }
 }
}