Click here to Skip to main content
15,885,366 members
Articles / Programming Languages / C#

Asynchronous Context Processor

Rate me:
Please Sign up or sign in to vote.
4.41/5 (22 votes)
25 Aug 200414 min read 94.4K   732   45  
Asynchronous message processing infrastructure for .NET applications.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Configuration;

using ACP;

namespace ACPDemo
{
	delegate void DisplayData(IMessage msg);
	/// <summary>
	/// Summary description for ACPDemo.
	/// </summary>
	public class ACPDemo : System.Windows.Forms.Form
	{
		private System.Windows.Forms.DataGrid dataGrid1;
		private System.Windows.Forms.Button LoadAuthors;
		private System.Windows.Forms.DataGrid dataGrid2;
		private System.Windows.Forms.Button LoadPubs;
		private System.Windows.Forms.Button clearAuthors;
		private System.Windows.Forms.Button clearPubs;
		private System.Windows.Forms.ProgressBar progressBar1;
		private System.Windows.Forms.ProgressBar progressBar2;
		private System.Timers.Timer timer1;
		private System.Timers.Timer timer2;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		// Context IDs for 'Author' Data retrieval Context & 'Publisher'
		// Data retrieval Context
		private string authCtx = null;
		private string pubsCtx = null;

		public ACPDemo()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <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.LoadAuthors = new System.Windows.Forms.Button();
			this.dataGrid1 = new System.Windows.Forms.DataGrid();
			this.dataGrid2 = new System.Windows.Forms.DataGrid();
			this.LoadPubs = new System.Windows.Forms.Button();
			this.clearAuthors = new System.Windows.Forms.Button();
			this.clearPubs = new System.Windows.Forms.Button();
			this.progressBar1 = new System.Windows.Forms.ProgressBar();
			this.progressBar2 = new System.Windows.Forms.ProgressBar();
			this.timer1 = new System.Timers.Timer();
			this.timer2 = new System.Timers.Timer();
			((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.dataGrid2)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.timer1)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.timer2)).BeginInit();
			this.SuspendLayout();
			// 
			// LoadAuthors
			// 
			this.LoadAuthors.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
			this.LoadAuthors.Location = new System.Drawing.Point(504, 208);
			this.LoadAuthors.Name = "LoadAuthors";
			this.LoadAuthors.Size = new System.Drawing.Size(88, 23);
			this.LoadAuthors.TabIndex = 0;
			this.LoadAuthors.Text = "Load &Authors";
			this.LoadAuthors.Click += new System.EventHandler(this.LoadAuthors_Click);
			// 
			// dataGrid1
			// 
			this.dataGrid1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.dataGrid1.DataMember = "";
			this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
			this.dataGrid1.Location = new System.Drawing.Point(0, 0);
			this.dataGrid1.Name = "dataGrid1";
			this.dataGrid1.RowHeaderWidth = 13;
			this.dataGrid1.Size = new System.Drawing.Size(596, 192);
			this.dataGrid1.TabIndex = 1;
			// 
			// dataGrid2
			// 
			this.dataGrid2.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.dataGrid2.DataMember = "";
			this.dataGrid2.HeaderForeColor = System.Drawing.SystemColors.ControlText;
			this.dataGrid2.Location = new System.Drawing.Point(0, 240);
			this.dataGrid2.Name = "dataGrid2";
			this.dataGrid2.RowHeaderWidth = 13;
			this.dataGrid2.Size = new System.Drawing.Size(596, 192);
			this.dataGrid2.TabIndex = 3;
			// 
			// LoadPubs
			// 
			this.LoadPubs.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
			this.LoadPubs.Location = new System.Drawing.Point(504, 440);
			this.LoadPubs.Name = "LoadPubs";
			this.LoadPubs.Size = new System.Drawing.Size(88, 23);
			this.LoadPubs.TabIndex = 2;
			this.LoadPubs.Text = "Load &Publishers";
			this.LoadPubs.Click += new System.EventHandler(this.LoadPubs_Click);
			// 
			// clearAuthors
			// 
			this.clearAuthors.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
			this.clearAuthors.Location = new System.Drawing.Point(400, 208);
			this.clearAuthors.Name = "clearAuthors";
			this.clearAuthors.Size = new System.Drawing.Size(88, 23);
			this.clearAuthors.TabIndex = 4;
			this.clearAuthors.Text = "&Clear";
			this.clearAuthors.Click += new System.EventHandler(this.clearAuthors_Click);
			// 
			// clearPubs
			// 
			this.clearPubs.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
			this.clearPubs.Location = new System.Drawing.Point(400, 440);
			this.clearPubs.Name = "clearPubs";
			this.clearPubs.Size = new System.Drawing.Size(88, 23);
			this.clearPubs.TabIndex = 5;
			this.clearPubs.Text = "C&lear";
			this.clearPubs.Click += new System.EventHandler(this.clearPubs_Click);
			// 
			// progressBar1
			// 
			this.progressBar1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.progressBar1.Location = new System.Drawing.Point(8, 212);
			this.progressBar1.Name = "progressBar1";
			this.progressBar1.Size = new System.Drawing.Size(384, 16);
			this.progressBar1.TabIndex = 6;
			// 
			// progressBar2
			// 
			this.progressBar2.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.progressBar2.Location = new System.Drawing.Point(8, 443);
			this.progressBar2.Name = "progressBar2";
			this.progressBar2.Size = new System.Drawing.Size(384, 16);
			this.progressBar2.TabIndex = 7;
			// 
			// timer1
			// 
			this.timer1.SynchronizingObject = this;
			this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Elapsed);
			// 
			// timer2
			// 
			this.timer2.SynchronizingObject = this;
			this.timer2.Elapsed += new System.Timers.ElapsedEventHandler(this.timer2_Elapsed);
			// 
			// ACPDemo
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(600, 485);
			this.Controls.Add(this.progressBar2);
			this.Controls.Add(this.progressBar1);
			this.Controls.Add(this.clearPubs);
			this.Controls.Add(this.clearAuthors);
			this.Controls.Add(this.dataGrid2);
			this.Controls.Add(this.LoadPubs);
			this.Controls.Add(this.dataGrid1);
			this.Controls.Add(this.LoadAuthors);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
			this.Name = "ACPDemo";
			this.Text = "ACPDemo";
			this.Closing += new System.ComponentModel.CancelEventHandler(this.ACPDemo_Closing);
			((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.dataGrid2)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.timer1)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.timer2)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new ACPDemo());
		}

		private void LoadAuthors_Click(object sender, System.EventArgs e)
		{
			// Get a ContextManager
			IContextManager	ctxMgr = ProviderManager.GetContextManager("Generic");
			if (ctxMgr != null)
			{
				// Create a Context if it does not already exists
				IContext ctx = null;
				if (authCtx != null)
				{
					ctx = ctxMgr.GetContext(authCtx);
				}
				else
				{
					ctx = ctxMgr.CreateContext(ContextMsgQType.ContextLevel,"DBMsgProc");
					authCtx = ctx.ID;
					ctx.MessageProcessor.MessageProcessingCompleted = new MessageProcessCompleteHandler(MessageProcessor_MessageProcessingCompleted);
				}
				// Get a ContextProcessorPoolManager
				IContextProcessorPoolManager cppMgr = ProviderManager.GetContextProcessorPoolManager("Generic");
				if (cppMgr != null)
				{
					IContextProcessorPool cpp = cppMgr.CreateContextProcessorPool("Test",ContextProcessorPoolMode.Push);
					if (cpp != null)
					{
						// Assign a ContextWorker to the Context
						cpp.AssignContext(ctx);
						
						// Create a DB message and queue it using context
						DBMessage msg = new DBMessage();
						
						string conStr = ConfigurationSettings.AppSettings["conStr"];
						string query = ConfigurationSettings.AppSettings["query1"];						
						DBRequest request = new DBRequest(conStr,query,DBRequest.DBQueryType.Select);

						msg.Request = request;

						// Queue the Message to be processed asynchronously by 
						// the Context Processor assigned to the context
						ctx.QueueMessage(msg,true);
						timer1.Enabled = true;

						// Wait on our select operation to complete
						while(ctx.WaitOnAsyncOperations(1) == false)
						{
							Application.DoEvents();
						}
					}
				}
			}
		}

		private void MessageProcessor_MessageProcessingCompleted(IMessage msg)
		{
			object[] args = new object[] { msg };
			this.Invoke(new DisplayData(this.ShowDataset),args);
		}

		private void ShowDataset(IMessage msg)
		{
			if (msg.Response != null)
			{
				DataSet ds = (DataSet)msg.Response;
				dataGrid1.DataSource = ds.Tables[0];
			}
			else
				MessageBox.Show(msg.Fault.ToString());
			
			timer1.Enabled = false;
			progressBar1.Value = progressBar1.Maximum;
			progressBar1.Value = 0;
		}

		private void ACPDemo_Closing(object sender, System.ComponentModel.CancelEventArgs e)
		{
			ProviderManager.ClearContextProcessorPoolManagers();
		}

		private void LoadPubs_Click(object sender, System.EventArgs e)
		{
			// Get a ContextManager
			IContextManager	ctxMgr = ProviderManager.GetContextManager("Generic");
			if (ctxMgr != null)
			{
				// Create a Context if it does not already exists
				IContext ctx = null;
				if (pubsCtx != null)
				{
					ctx = ctxMgr.GetContext(pubsCtx);
				}
				else
				{
					ctx = ctxMgr.CreateContext(ContextMsgQType.ContextLevel,"DBMsgProc");
					pubsCtx = ctx.ID;
					ctx.MessageProcessor.MessageProcessingCompleted = new MessageProcessCompleteHandler(MessageProcessor_MessageProcessingCompleted_Pubs);
				}
				// Get a ContextProcessorPoolManager
				IContextProcessorPoolManager cppMgr = ProviderManager.GetContextProcessorPoolManager("Generic");
				if (cppMgr != null)
				{
					IContextProcessorPool cpp = cppMgr.CreateContextProcessorPool("Test",ContextProcessorPoolMode.Push);
					if (cpp != null)
					{
						// Assign a ContextWorker to the Context
						cpp.AssignContext(ctx);
						
						// Create a DB message and queue it using context
						DBMessage msg = new DBMessage();
						
						string conStr = ConfigurationSettings.AppSettings["conStr"];;
						string query = ConfigurationSettings.AppSettings["query2"];;						
						DBRequest request = new DBRequest(conStr,query,DBRequest.DBQueryType.Select);

						msg.Request = request;
						
						// Queue the Message to be processed asynchronously by 
						// the Context Processor assigned to the context
						ctx.QueueMessage(msg,true);
						timer2.Enabled = true;

						// Wait on our select operation to complete
						while(ctx.WaitOnAsyncOperations(1) == false)
						{
							Application.DoEvents();
						}
					}
				}
			}		
		}

		private void MessageProcessor_MessageProcessingCompleted_Pubs(IMessage msg)
		{
			object[] args = new object[] { msg };
			this.Invoke(new DisplayData(this.ShowDatasetPubs),args);
		}

		private void ShowDatasetPubs(IMessage msg)
		{
			if (msg.Response != null)
			{
				DataSet ds = (DataSet)msg.Response;
				dataGrid2.DataSource = ds.Tables[0];
			}
			else
				MessageBox.Show(msg.Fault.ToString());
			
			timer2.Enabled = false;
			progressBar2.Value = progressBar1.Maximum;
			progressBar2.Value = 0;
		}

		private void clearAuthors_Click(object sender, System.EventArgs e)
		{
			dataGrid1.DataSource = null;
		}

		private void clearPubs_Click(object sender, System.EventArgs e)
		{
			dataGrid2.DataSource = null;
		}

		private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
		{
			if (progressBar1.Value == progressBar1.Maximum)
				progressBar1.Value = progressBar1.Minimum;
			
			if (timer1.Enabled == true)
				progressBar1.Value += 1;				
			else
				progressBar1.Value = 0;
		}

		private void timer2_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
		{
			if (progressBar2.Value == progressBar2.Maximum)
				progressBar2.Value = progressBar2.Minimum;
			
			if (timer1.Enabled == true)
				progressBar2.Value += 1;				
			else
				progressBar2.Value = 0;
		}
	}
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Architect
India India
Software Professional with 14+ Years of experience in design & development of server products using Microsoft Technologies.

Woked/Working on server side product development using Managed C++ & C#, including Thread pools, Asynchronous Procedure Calls (APC), Inter Process Communication (IPC) using named pipes, Lock Free data structures in C++ & .Net, etc.

Comments and Discussions