Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

Automatic Implementation of the Event-Based Asynchronous Pattern

, 26 Nov 2008
Implement the event-based asynchronous pattern automatically with this code generator
asyncgen_demo.zip
asyncgen_demo
ClassLibrary1
bin
Debug
ClassDiagram.cd
Properties
Client
bin
Debug
Properties
Settings.settings
Server
bin
Debug
Properties
asyncgen_src.zip
asyncgen_src
AsyncClientGeneratorLib
asyncgen.snk
ClassDiagram.cd
fxReflection.proj
Properties
Properties
TestAssembly
Properties
TestClientCpp
app.ico
TestClientCSharp
Properties
TestClientVB
My Project
Application.myapp
Settings.settings
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Remoting;
using System.Diagnostics;
using System.Threading;
using AsyncGen;

namespace EventBasedAsync
{
	public partial class Client : Form
	{
		#region Construction

		public Client()
		{
			InitializeComponent();
			WellKnownClientTypeEntry[] wellKnownClientTypes = RemotingConfiguration.GetRegisteredWellKnownClientTypes();
			foreach (WellKnownClientTypeEntry ct in wellKnownClientTypes)
			{
				if (typeof(IServer).IsAssignableFrom(ct.ObjectType))
				{
					server = (IServer)Activator.GetObject(ct.ObjectType, ct.ObjectUrl);
                    proxy = new Proxy(server);
					proxy.DoSomethingCompleted += proxy_DoSomethingCompleted;
					proxy.DoSomethingProgressChanged += proxy_DoSomethingProgressChanged;
					break;
				}
			}
			ThreadPool.SetMinThreads(2, 20);
		}

		#endregion

		#region Private Event Handlers

		private void proxy_DoSomethingProgressChanged(object sender, ProgressChangedEventArgs args)
		{
			ListViewItem task = listView1.Items[args.UserState.ToString()];
			ProgressBar pb = (ProgressBar) listView1.GetEmbeddedControl((Guid) args.UserState);
			pb.Value = args.ProgressPercentage;
			task.SubItems[1].Text = string.Format("{0}%", args.ProgressPercentage);
			task.SubItems[2].Text = "Active";
		}

		private void proxy_DoSomethingCompleted(object sender, AsyncCompletedEventArgs<int> args)
		{
			Debug.Assert(!Thread.CurrentThread.IsThreadPoolThread);

			if (args.Error != null)
			{
				MessageBox.Show(args.Error.StackTrace, args.Error.Message);
			}
			else if (args.Cancelled)
			{
				ListViewItem task = listView1.Items[args.UserState.ToString()];
				task.SubItems[2].Text = "Cancelled";
				task.BackColor = SystemColors.Window;
				task.ForeColor = SystemColors.GrayText;
				task.Checked = true;
			}
			else
			{
				ListViewItem task = listView1.Items[args.UserState.ToString()];
				task.SubItems[2].Text = "Completed";
                task.SubItems[4].Text = args.Output.ToString();
				task.BackColor = SystemColors.Window;
				task.ForeColor = SystemColors.GrayText;
				task.Checked = true;
			}
		}

		private void button1_Click(object sender, EventArgs e)
		{
			listView1.SuspendLayout();
			try
			{
				for (int i = 0; i < newWorkItemsUpDownControl.Value; i++)
				{
					int n = random.Next(10);
					Guid taskId = Guid.NewGuid();
					ListViewItem item = new ListViewItem(
						new string[] 
					{
						DateTime.Now.ToString(),
						"0%",
						"Pending",
						n.ToString(),
						string.Empty
					});
					item.Name = taskId.ToString();

					#region Embed a progress bar in the ListView row.

                    ProgressBar pb = new ProgressBar();
                    pb.BackColor = SystemColors.Control;
                    pb.Style = ProgressBarStyle.Continuous;
                    listView1.AddEmbeddedControl(taskId, pb, item, 1);

					#endregion

					listView1.Items.Add(item);

					listView1.EnsureVisible(item.Index);

					proxy.DoSomethingAsync(n, taskId);
				}
			}
			finally
			{
				listView1.ResumeLayout();
			}
		}

		private void purgeCompletedToolStripMenuItem_Click(object sender, EventArgs e)
		{
			int i = 0;
			while (i < listView1.Items.Count)
			{
				if (listView1.Items[i].Checked)
				{
					listView1.Items.RemoveAt(i);
				}
				else
				{
					++i;
				}
			}
		}

		private void cancelSelectedToolStripMenuItem_Click(object sender, EventArgs e)
		{
			foreach (ListViewItem item in listView1.SelectedItems)
			{
				if (!item.Checked)
                {
                    proxy.DoSomethingAsyncCancel(new Guid(item.Name));
                }
			}
		}

		#endregion

		#region Private Fields

		private IServer server;
		private Proxy proxy;
		private Random random = new Random();

		#endregion
	}
}

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)

Share

About the Author

Ron A Inbar
Software Developer (Senior) Philips Healthcare
Israel Israel
I got my B.Sc. in Mathematics and Computer Science from Tel Aviv University in 1997. Since then I have developed software in UNIX, Win32 and .NET. I currently live in Haifa.

| Advertise | Privacy | Mobile
Web03 | 2.8.140905.1 | Last Updated 26 Nov 2008
Article Copyright 2008 by Ron A Inbar
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid