This blog is about the dotnet.all types of codes,news about dotnet including asp.net,vb.net,c# and know about new dotnet technology.programing in asp.net,vb.net,c#, ajax, AJAX tech support for .net and discuss the new technology in dotnet.ncluding asp.net,vb.net,c# and know about new dotnet technology.programing in asp.net,vb.net,c#, ajax, AJAX tech support for .net and discuss the new technology in dotnet.asp.net programming,dot net programming,dotnet programs,dotnet source code,source code.

Free Hosting

Free Hosting

Thursday, October 30, 2008

CCRing - C-Sharp(C# Code)

Introduction
I often revisit applications I've written to improve areas of the code with ideas and lessons I pickup over time. There seems to always be one primary goal and this is to improve performance. When it comes to improving performance there are many things you can do but in the end you'll always look to multithreading. It's theoretically the simplest suggested concept but not always the easiest to implement. If you've ever run into resource sharing problems you'll know what I mean and although there are many articles on how to do it, it doesn't always mesh with every solution.

Some time ago I came across something called the CCR, it was like magic code created by two wizards Jeff Richter and George Chrysanthakopoulos. Part of the magic was to properly roll the syllables in Chrysanthakopoulos neatly off your tongue in one breath and when you get past that you'll see the light at the end of the multithreaded hallway of horrors. This managed DLL is packed with oodles of multithreaded fun and provides many levels of simplicity to common threading complexities. In other words, if you want to improve performance of your applications by implementing a multithreaded layer then you need to live and breathe the CCR. For some great background and fun grab some popcorn and visit Jeff and George at this link.

Background
After watching the video cast you should come away with some confidence and revelation along with some courage to start using the CCR. So you'll open up your latest project and... where to you start? Well one place you can start is by creating a simple asynchronous logger. Most applications I design have varying levels of logging for production diagnosis but if you don't use a threaded model when utilizing your logger class then you've created blocking code and obvious room for improvement. So to get you started I'll show you how to implement a CCR'd logger class that writes to a local file. There are many ways to log data but for this demo I'm using simple local logging. You will most likely be interested in this article; it will explain the many faces of the CCR.

Using the Code
The following code can be dropped into your application and be utilized right away and although basic it can act as a replacement for any logging methods you currently implement.

The first thing we need to do is to new up something called a Dispatcher, think of this as the thread "pool". Notice the "1", this means we only want one thread handling these calls therefore all "posts" to the class will execute async but sequential. If you're writing to a SQL database you can try increasing this number but be aware that data may not arrive sequentially! When utilizing a dispatcher for other non sequential tasks, try increasing this number.

//Create Dispatcher
private static readonly Dispatcher _logDispatcher = new Dispatcher(1, "LogPool");
Secondly you'll want a DispatcherQueue. The DQ manages your list of delegates to methods, methods you need to execute when needed.

//Create Dispatch Queue
private static DispatcherQueue _logDispatcherQueue;
Next you need a PORT, ports are like input queues. You'll "post" to ports to invoke your registered methods.

//Message Port
private static readonly Port _logPort = new Port();
Now for the class, dont' forget to include the CCR in the directives!

Collapse
using System;
using System.IO;
using System.Threading;
using Microsoft.Ccr.Core;

namespace CCRing_Demo
{
public static class DataLogger
{
//Create Dispatcher
private static readonly Dispatcher _logDispatcher = new Dispatcher(1,
ThreadPriority.Normal, false, "LogPool");

//Create Dispatch Queue
private static DispatcherQueue _logDispatcherQueue;

//Message Port
private static readonly Port _logPort = new Port();

//Fields
private static string _logFileName;

private static void Init()
{
_logDispatcherQueue = new DispatcherQueue("LogDispatcherQueue",
_logDispatcher);
Arbiter.Activate(_logDispatcherQueue, Arbiter.Receive(true, _logPort,
WriteMessage));

_logFileName = "DMT_Message_Log_" + String.Format("{0:yyMMddHHmmss}",
DateTime.Now) + ".log";
}

private static void WriteMessage(string messageString)
{
var sw = new StreamWriter(_logFileName, true);
sw.WriteLine("[" + String.Format("{0:HH:mm:ss tt}", DateTime.Now) + "]" +
messageString);
sw.Close();
}


public static void Log(string messageString)
{
if (String.IsNullOrEmpty(_logFileName))
Init();
_logPort.Post(messageString);
}

//Any thread tasks still running?
private static bool PendingJobs
{
get
{
return (_logDispatcher.PendingTaskCount > 0) ? true : false;
}
}

//Since we are not using background threading we need to add this method to
//dispose the DQ for application end
public static void StopLogger()
{
while (PendingJobs){Thread.Sleep(100);}
_logFileName = null;
_logDispatcherQueue.Dispose();
}
}
}


SOURCE CODE:

CLICKHERE TO DOWNLOAD CCRing_Demo - 97.73 KB

0 comments:

dotnet(.Net) Project Source code Downloads and Tutorials

Email Subscrption



Enter your email address:

Delivered by FeedBurner

Feedburner Count

Unique Visitor

Design by araba-cı | MoneyGenerator Blogger Template by GosuBlogger