Click here to Skip to main content
15,892,161 members
Articles / Web Development / ASP.NET

Access Web Services Asynchronously in .NET Design Patterns

Rate me:
Please Sign up or sign in to vote.
4.92/5 (25 votes)
4 Jul 2006CPOL11 min read 131.2K   803   106  
Discussing the implementation of the Begin/End pattern (.NET 1) and the event-driven model (.NET 2).
using System;
using System.Windows.Forms;
using System.Net;
using System.Runtime.Remoting.Messaging;
using WsClient1.WsTestRef1;

namespace WsClient1
{
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Button buttonCancel;
        private System.Windows.Forms.Button buttonGetQuote;
        private System.Windows.Forms.TextBox textBoxResult;
        private System.Windows.Forms.ComboBox comboBoxSymb;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.TextBox textBoxTimeout;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        long _tmStart;
        private Service _wsProxy1;

        public Form1()
        {
            InitializeComponent();
            comboBoxSymb.SelectedIndex = 0;
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null) 
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.label1 = new System.Windows.Forms.Label();
			this.buttonCancel = new System.Windows.Forms.Button();
			this.comboBoxSymb = new System.Windows.Forms.ComboBox();
			this.buttonGetQuote = new System.Windows.Forms.Button();
			this.textBoxResult = new System.Windows.Forms.TextBox();
			this.label2 = new System.Windows.Forms.Label();
			this.textBoxTimeout = new System.Windows.Forms.TextBox();
			this.SuspendLayout();
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(8, 16);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(80, 16);
			this.label1.TabIndex = 0;
			this.label1.Text = "Stock Symbol:";
			// 
			// buttonCancel
			// 
			this.buttonCancel.Location = new System.Drawing.Point(176, 40);
			this.buttonCancel.Name = "buttonCancel";
			this.buttonCancel.Size = new System.Drawing.Size(80, 24);
			this.buttonCancel.TabIndex = 4;
			this.buttonCancel.Text = "Cancel";
			this.buttonCancel.Click += new System.EventHandler(this.buttonCancel_Click);
			// 
			// comboBoxSymb
			// 
			this.comboBoxSymb.Items.AddRange(new object[] {
															  "ELNK",
															  "MSFT",
															  "IBM",
															  "GOOG",
															  "SUNW"});
			this.comboBoxSymb.Location = new System.Drawing.Point(88, 11);
			this.comboBoxSymb.Name = "comboBoxSymb";
			this.comboBoxSymb.Size = new System.Drawing.Size(80, 21);
			this.comboBoxSymb.TabIndex = 5;
			this.comboBoxSymb.TextChanged += new System.EventHandler(this.comboBoxSymb_TextChanged);
			this.comboBoxSymb.SelectedIndexChanged += new System.EventHandler(this.comboBoxSymb_SelectedIndexChanged);
			// 
			// buttonGetQuote
			// 
			this.buttonGetQuote.Location = new System.Drawing.Point(176, 8);
			this.buttonGetQuote.Name = "buttonGetQuote";
			this.buttonGetQuote.Size = new System.Drawing.Size(80, 24);
			this.buttonGetQuote.TabIndex = 6;
			this.buttonGetQuote.Text = "Get Quote";
			this.buttonGetQuote.Click += new System.EventHandler(this.buttonGetQuote_Click);
			// 
			// textBoxResult
			// 
			this.textBoxResult.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(255)), ((System.Byte)(192)));
			this.textBoxResult.BorderStyle = System.Windows.Forms.BorderStyle.None;
			this.textBoxResult.Location = new System.Drawing.Point(8, 72);
			this.textBoxResult.Name = "textBoxResult";
			this.textBoxResult.Size = new System.Drawing.Size(248, 13);
			this.textBoxResult.TabIndex = 7;
			this.textBoxResult.Text = "";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(8, 40);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(80, 16);
			this.label2.TabIndex = 8;
			this.label2.Text = "Timeout (ms):";
			// 
			// textBoxTimeout
			// 
			this.textBoxTimeout.Location = new System.Drawing.Point(88, 40);
			this.textBoxTimeout.Name = "textBoxTimeout";
			this.textBoxTimeout.Size = new System.Drawing.Size(64, 20);
			this.textBoxTimeout.TabIndex = 9;
			this.textBoxTimeout.Text = "3000";
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(264, 92);
			this.Controls.Add(this.textBoxTimeout);
			this.Controls.Add(this.textBoxResult);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.buttonGetQuote);
			this.Controls.Add(this.comboBoxSymb);
			this.Controls.Add(this.buttonCancel);
			this.Controls.Add(this.label1);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
			this.Name = "Form1";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Web Service Client1";
			this.ResumeLayout(false);

		}
		#endregion

        [STAThread]
        static void Main() 
        {
            Application.Run(new Form1());
        }

        private void comboBoxSymb_TextChanged(object sender, EventArgs e)
        {
            textBoxResult.Text = "";
        }

        private void comboBoxSymb_SelectedIndexChanged(object sender, EventArgs e)
        {
            textBoxResult.Text = "";
        }

        private void buttonGetQuote_Click(object sender, System.EventArgs e)
        {
			buttonGetQuote.Enabled = false;
            textBoxResult.Text = "";
            _tmStart = DateTime.Now.Ticks;

            string symbol = comboBoxSymb.Text.ToUpper();
            int timeout = int.Parse(textBoxTimeout.Text);
    
            _wsProxy1 = new Service();
            _wsProxy1.BeginGetStock(symbol, timeout, new AsyncCallback(OnGetStock), symbol);    
        }

        private void OnGetStock(IAsyncResult ar)
        {
            string symbol = (string)ar.AsyncState;
            string result;
            
            try 
            {
                double value = _wsProxy1.EndGetStock(ar);
                if (value ==0)
                    result = "Invalid, " + "'" +symbol +"'";
                else
                if (value <0)
                    result = "TimeOut, " + "[" +symbol +"]";
                else
                    result = "OK, " + value.ToString("F");
            }
            catch (WebException e)
            {
                if (e.Status == WebExceptionStatus.RequestCanceled)
                    result = "Cancelled, " + "<" +symbol +">";
                else
                    result = "Exception, " + e.Message;
            }
            catch (Exception e)
            {
                result = "Exception, " + e.Message;
            }

            //textBoxResult.Text = result + ",  (" + (DateTime.Now.Ticks -	 _tmStart) / 10000 + " ms)";
            textBoxResult.Invoke(new ShowResultDelegate(ShowResult), new object[] { result });
            _wsProxy1 = null;
			buttonGetQuote.Enabled = true;
		}
            
        private void buttonCancel_Click(object sender, System.EventArgs e)
        {
            if (_wsProxy1 != null)
                _wsProxy1.Abort();
        }

        private delegate void ShowResultDelegate(string str);
        private void ShowResult(string str)
        {
            textBoxResult.Text = str + ",  (" + (DateTime.Now.Ticks - _tmStart) / 10000 + " ms)";
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions