Click here to Skip to main content
15,879,326 members
Articles / Programming Languages / Visual Basic

Automatic Implementation of the Event-Based Asynchronous Pattern

Rate me:
Please Sign up or sign in to vote.
4.78/5 (32 votes)
26 Nov 2008CPOL20 min read 63.1K   912   101  
Implement the event-based asynchronous pattern automatically with this code generator
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting;
using System.Diagnostics;
using System.Threading;

namespace EventBasedAsync
{
	class Program
	{
		private const string InvalidArgumentMessage = "Invalid argument.  Thread pool limits were not changed.";

		static void Main(string[] args)
		{
			Process currentProcess = Process.GetCurrentProcess();
			RemotingConfiguration.Configure(currentProcess.MainModule.FileName + ".config", false);

			Server server = new Server();
			RemotingServices.Marshal(server, "Server.rem");

			for (; ; )
			{
				Console.Write("> ");
				string cmd = Console.ReadLine().ToLower();
				string[] tokens = cmd.Split(' ');
				int workerThreads, completionPortThreads, arg;
				if (tokens.Length == 1 && tokens[0] == "q")
				{
					Console.WriteLine("Goodbye!");
					Thread.Sleep(1000);
					break;
				}
				else if (tokens.Length == 1 && tokens[0] == "g")
				{
					int minWorkerThreads, minCompletionPortThreads, maxWorkerThreads, maxCompletionPortThreads;
					ThreadPool.GetMinThreads(out minWorkerThreads, out minCompletionPortThreads);
					ThreadPool.GetMaxThreads(out maxWorkerThreads, out maxCompletionPortThreads);
					Console.WriteLine("Component           Min        Max");
					Console.WriteLine("----------------------------------");
					Console.WriteLine("Worker            {0,5}      {1,5}", minWorkerThreads, maxWorkerThreads);
					Console.WriteLine("IO Completion     {0,5}      {1,5}", minCompletionPortThreads, maxCompletionPortThreads);
				}
				else if (tokens.Length == 2 && tokens[0] == "sminw" && int.TryParse(tokens[1], out arg))
				{
					ThreadPool.GetMinThreads(out workerThreads, out completionPortThreads);
					if (!ThreadPool.SetMinThreads(arg, completionPortThreads))
					{
						Console.Error.WriteLine(InvalidArgumentMessage);
					}
				}
				else if (tokens.Length == 2 && tokens[0] == "sminc" && int.TryParse(tokens[1], out arg))
				{
					ThreadPool.GetMinThreads(out workerThreads, out completionPortThreads);
					if (!ThreadPool.SetMinThreads(workerThreads, arg))
					{
						Console.Error.WriteLine(InvalidArgumentMessage);
					}
				}
				else if (tokens.Length == 2 && tokens[0] == "smaxw" && int.TryParse(tokens[1], out arg))
				{
					ThreadPool.GetMaxThreads(out workerThreads, out completionPortThreads);
					if (!ThreadPool.SetMaxThreads(arg, completionPortThreads))
					{
						Console.Error.WriteLine(InvalidArgumentMessage);
					}
				}
				else if (tokens.Length == 2 && tokens[0] == "smaxc" && int.TryParse(tokens[1], out arg))
				{
					ThreadPool.GetMaxThreads(out workerThreads, out completionPortThreads);
					if (!ThreadPool.SetMaxThreads(workerThreads, arg))
					{
						Console.Error.WriteLine(InvalidArgumentMessage);
					}
				}
				else
				{
					Console.Error.WriteLine("Syntax error!\a");
				}
			}
		}
	}
}

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
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.

Comments and Discussions