using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace Harbormist { public partial class MainForm : Form { public MainForm() { InitializeComponent(); label1.BackColor = color1; label2.BackColor = color1; label3.BackColor = color1; } Color color1 = Color.Aqua; Color color2 = Color.Pink; void toggleLabel(System.Windows.Forms.Label myLabel) { if (myLabel.BackColor == color1) myLabel.BackColor = color2; else myLabel.BackColor = color1; } // syncronous (normal) call to regular (non-slow) service private void button1_Click(object sender, EventArgs e) { com.harbormist.Quote myQuoteService = new com.harbormist.Quote(); textBox1.Text = myQuoteService.GetQuote(); toggleLabel(label1); } // syncronous (normal) call to slow service (2-second delay) private void button2_Click(object sender, EventArgs e) { com.harbormist.slow.Quote mySlowQuoteService = new com.harbormist.slow.Quote(); textBox1.Text = mySlowQuoteService.GetQuote(); toggleLabel(label2); } // Asyncronous ("fire and forget") call to slow service (2-second delay) // returns control to GUI immediately; service calls back later with answer private void button3_Click(object sender, EventArgs e) { com.harbormist.slow.Quote mySlowQuoteService = new com.harbormist.slow.Quote(); mySlowQuoteService.GetQuoteCompleted += new com.harbormist.slow.GetQuoteCompletedEventHandler(GetAnswer); mySlowQuoteService.GetQuoteAsync(); toggleLabel(label3); } // the callback method for the async call void GetAnswer(object sender, com.harbormist.slow.GetQuoteCompletedEventArgs e) { String myQuote = e.Result; textBox1.Text = myQuote; } } }