using System;
using System.Threading;
using System.Data;
using System.Data.SqlClient;

namespace Async
{
	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	class Class1
	{
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main(string[] args)
		{
			SqlConnection conn = new SqlConnection("server=.;integrated security=sspi;database=AdventureWorks;async=true");
			SqlCommand cmd = new SqlCommand("select * from person.ContactType;waitfor delay '00:00:02'", conn);

      try
			{
				conn.Open();
				IAsyncResult ar = cmd.BeginExecuteReader();

				Console.WriteLine("After BeginExecute");
				while (!ar.IsCompleted)
				{
					Console.Write(".");
					Thread.Sleep(250);
				}

				SqlDataReader rdr = cmd.EndExecuteReader(ar);
				while (rdr.Read())
					Console.WriteLine(rdr[0]);
			}
			catch (Exception e)
			{
				Console.WriteLine(e.Message);
				Console.WriteLine(e.StackTrace);
			}
			finally
			{
				conn.Dispose();
				cmd.Dispose();
			}
			Console.ReadLine();
		}
	}
}
