Click here to Skip to main content
15,886,816 members
Articles / Database Development / SQL Server

Flexible and Plug-in-based .NET Application using Provider Pattern

,
Rate me:
Please Sign up or sign in to vote.
4.67/5 (26 votes)
1 Dec 200511 min read 111.6K   1.2K   161  
This paper demonstrates creating a flexible, extensible, plug-in-based .NET application (JOM - Smart Job Manager). JOM is an asynchronous job processing engine built using the MS Provider design pattern and the .NET technology available today.
	
// =====================================================================================
// Copyright � 2005 by Shahed Khan. All rights are reserved.
// 
// If you like this code then feel free to go ahead and use it.
// The only thing I ask is that you don't remove or alter my copyright notice.
//
// Your use of this software is entirely at your own risk. I make no claims or
// warrantees about the reliability or fitness of this code for any particular purpose.
// If you make changes or additions to this code please mark your code as being yours.
// 
// website , email shahed.khan@gmail.com
// =====================================================================================

using System;
using System.Messaging;
using SmartJobManager;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace ClientApplication
{
	class QueuedClient
	{
		static string QueuePath = @".\Private$\jobmanagerTest";		

		public string ErrorMessage = string.Empty;
		public bool IsErrorExists = false;
		public  void QueueSimpleJob()
		{			
			Job job = new Job(0,0,DateTime.Now,DateTime.Now,"SimpleJobManagerProvider");
			JobDetailCollection jobDetails = new JobDetailCollection();
			JobDetail jobDetail = new JobDetail(0,0,"Copyright","Use at your own risk.");						
			jobDetails.Add(jobDetail);
			jobDetail = new JobDetail(0,0,"PersonName","Shahed Khan");						
			jobDetails.Add(jobDetail);
			jobDetail = new JobDetail(1,0,"Email","shahed.khan@gmail.com");
			jobDetails.Add(jobDetail);

			job.JobDetailCollection = jobDetails;
			QueueJob(job, QueuePath);
		}

		public  void QueueScreenCaptureImageJob()
		{			
			Job job = new Job(0,0,DateTime.Now,DateTime.Now,"ScreencaptureJobManagerProvider");
			JobDetailCollection jobDetails = new JobDetailCollection();
			 
			JobDetail jobDetail = new JobDetail(1,0,"CaptureImage",PerformCapture());
			jobDetails.Add(jobDetail);
			job.JobDetailCollection = jobDetails;
			QueueJob(job, QueuePath);
		}

		public  void QueueTextToFileJob(string text)
		{			
			if (text.Length < 1)
				text = "No Text Entered";
			Job job = new Job(0,0,DateTime.Now,DateTime.Now,"TextToFileJobManagerProvider");
			JobDetailCollection jobDetails = new JobDetailCollection();
			JobDetail jobDetail = new JobDetail(0,0,"TextToWrite",text);						
			jobDetails.Add(jobDetail);
			
			job.JobDetailCollection = jobDetails;
			QueueJob(job, QueuePath);
		}

		public  void QueueTextToImageJob(string text)
		{	
			if (text.Length < 1)
				text = "No Text Entered";
			Job job = new Job(0,0,DateTime.Now,DateTime.Now,"TextToImageJobManagerProvider");
			JobDetailCollection jobDetails = new JobDetailCollection();
			JobDetail jobDetail = new JobDetail(0,0,"TextToWrite",text);						
			jobDetails.Add(jobDetail);
			
			job.JobDetailCollection = jobDetails;
			QueueJob(job, QueuePath);
		}

		
		private  void QueueJob(Job job, string destinationQueue)
		{
			try
			{
				// open the queue
				MessageQueue mq = new MessageQueue(destinationQueue);
				// set the message to durable.
				mq.DefaultPropertiesToSend.Recoverable = true;
				// set the formatter to Binary, default is XML
				//mq.Formatter = new BinaryMessageFormatter();
				// send the job object
				mq.Send(job, "Job Message Test");
				mq.Close();
			}
			catch (Exception e)
			{
				//Console.WriteLine(e.ToString());
				this.ErrorMessage = e.ToString();
				this.IsErrorExists = true;
			}
			
		}

		#region Perform ScreenCapture

		//Originally written by Michael Gold 04/15/2002 
		//http://www.c-sharpcorner.com//Code/2002/April/ScreenCaptureUtility.asp
		//I modified a bit to meet my needs

		[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
		private static extern IntPtr  CreateDC(
			string lpszDriver,        // driver name
			string lpszDevice,        // device name
			string lpszOutput,        // not used; should be NULL
			IntPtr lpInitData	// optional printer data
			);
		[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
		private static extern bool BitBlt(
			IntPtr hdcDest, // handle to destination DC
			int nXDest,  // x-coord of destination upper-left corner
			int nYDest,  // y-coord of destination upper-left corner
			int nWidth,  // width of destination rectangle
			int nHeight, // height of destination rectangle
			IntPtr hdcSrc,  // handle to source DC
			int nXSrc,   // x-coordinate of source upper-left corner
			int nYSrc,   // y-coordinate of source upper-left corner
			System.Int32 dwRop  // raster operation code
			);

		private   byte[] PerformCapture()
		{ 						

			//use the GDI call and create a DC to the whole display

			IntPtr dc1 = CreateDC("DISPLAY", null, null, (IntPtr)null);

			//create a Graphics object for the screen dc

			Graphics g1 = Graphics.FromHdc(dc1);

			//  create a compatible bitmap the size of the entire screen

			//MyImage = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, g1);
			Bitmap image = new Bitmap(300,300,g1);
			//  use the bitmap to create another Graphics surface  so we can  BitBlast into the bitmap

			Graphics g2 = Graphics.FromImage(image);

			//  Now go retrace our steps and get the device contexts for both the bitmap and the screen
			// Note:  Apparently you have to do this, and can't go directly from the aquired dc or exceptions are thrown
			//            When you try to release the dcs

			dc1 = g1.GetHdc();

			IntPtr dc2 = g2.GetHdc();

			//  Bit Blast  the screen into the Bitmap

			BitBlt(dc2, 0, 0, 300, 300, dc1, 0, 0, 13369376);

			//   Remember to release the dc's, otherwise problems down the road

			g1.ReleaseHdc(dc1);

			g2.ReleaseHdc(dc2);
			
			//Converting to byte[]
			MemoryStream memStream = new MemoryStream();
			image.Save(memStream, System.Drawing.Imaging.ImageFormat.Jpeg);
			int ImageSize = Convert.ToInt32(memStream.Length);
			byte[] ImageBytes = new byte[ImageSize];
			memStream.Position=0;
			memStream.Read(ImageBytes, 0, ImageSize);
			memStream.Close();
			return ImageBytes;
		}

		#endregion Perform ScreenCapture


	}
}

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
Web Developer
Australia Australia
I have been awarded MVP (Visual C#) for year 2007, 2008, 2009. I am a Microsoft Certified Application Developer (C# .Net). I currently live in Melbourne, Australia. I am a co-founder and core developer of Pageflakes www.pageflakes.com and Founder of Simplexhub, a highly experienced software development company based in Melbourne Australia and Dhaka, Bangladesh. Simplexhub.
My BLOG http://www.geekswithblogs.net/shahed
http://msmvps.com/blogs/shahed/Default.aspx.

Written By
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions