Many enterprise applications today require reliable message delivery mechanism. This is absolutely essential in financial transactions. There are a number of techniques available to achieve this including a Custom made Queuing server, the underlying protocol in that case would be Transfer Control Protocol TCP and it has to be made reliable by wrapping it in a layer of requests and acknowledgements, persisting all the requests and acknowledgements of a transaction in a database and applying rules for “Transaction Processing” as in a database. So the transaction is literally atomic and has many phases.This is sometimes difficult to achieve since implementing Sockets is itself a difficult proposition, it comes with the issues of Server client synchronization , memory management with multiple threads and usually the proprietary protocol that’s used for message passing or a custom developed API which needs to be configured for server and clients.
I have recently used MSMQ and was able to achieve all this without the need to develop long API or a lot of development. Integrating it with C# was very easy and the message passing has been very reliable. All the threads, queue management and connection are in built into MSMQ .In this article I would describe the basic things that you need to do to integrate MSMQ into your projects. Before using any of the sample code makes sure that MSMQ is installed on your system. If not it can be installed from the Add/Remove Windows component option. Once it is installed it is displayed in the administrative applications list and then you can use the code similar to one described in various sections. Make sure you add following references in your application:
System.Messaging
System.Messaging.Formatters
Writing Strings or Formatted Objects to MSMQ:
The sample code below would create a Message Queue in the private queues list with the name TestMSMQ. All messages that you post would be posted in this queue. // Name of the Private Queu you want to post data to
String queueName = “.\private$\TestMSMQ”; // The string message tou want to send// The  objects of user defined classes can also be sent
// MSMQ holds the serizlized version when received the body // should be converted to the same class object again
String messageToSend = “This is a test message”; // the message object that would be send
System.Object obj = messageToSend; // Create a MSMQ message queuing transaction.
MessageQueueTransaction msgTran = new MessageQueueTransaction(); // Begin a MSMQ transaction.// If the Queue doesn’t exist create it Using API
msgTran.Begin(); if (MessageQueue.Exists(queueName))   
    msgQueue = new MessageQueue(queueName);
else   
    msgQueue = MessageQueue.Create(queueName);  

msgQueue = new MessageQueue(queueName); 
Message msg = new Message(); 
// Assign the message to the BODY Property
msg.Body = obj; // Associate a binary or an XML formatter
msg.Formatter = new System.Messaging.BinaryMessageFormatter();
 // Send the Message
msgQueue.Send(msg, msgTr);  // Commit the transaction.
msgTran.Commit(); 
Reading from MSMQ: 

Receiving a Message from the Message Queue is Simple. If a user defined datatype resides in the body of the message and the Message queue is accessed from different applciations, services the same type should exist in both the services , applications. This can be done by placing them in a project and adding a reference to the project in both the applications , then converting the object returned by the Body of the message to an instance of the required Type. Note:Receiving a message would dequeue the Message from MessageQueue. If you want to make sure that whenever a message is removed from the message queue a copy of it is saved  you can use the Journal queue by setting the use journal queue flag while posting to the message queue. However if you don’t want to dequeue the message an Enumerator can be obtained and be processed with a loop that peeks the Message Queue messages. Use Peek instead of Receive and Receive only when absolutely certain about removing the message from the queue. // process the online messages here
string onlineQueueName = .\private$\TestMSMQ”;              // initialize the Message Queue instance
MessageQueue msgQueue;msgQueue = new MessageQueue(onlineQueueName); System.Messaging.Message Msg = nullMsg = olMq.Receive();Note : If you might be interested in  working code for MSMQ or have some design and architecture ideas feel free to discuss or email me at hiasad@hotmail.com

Have you ever seen this exception and wondered “Huh !! wtf is that suppose to mean ?”

exception.jpg

Cross-thread operation not valid: Control ‘Form1′ accessed from a thread other than the thread it was created on.”?? Or you want to update a UI of a windows based application in real time. The classis scenario for this is the stock market applications or processing credit cards from windows based application or while making web service calls synchronously where at many places the real time updates to the UI is absolutely essential.As an application developer / designer you might have encountered situations where The UI of the application gets updated After a COM Component Call or a Web service call or some extensive processing with the data. If this takes too long the User Interface of your application stops responding and hangs which is the first thing people notice and criticize in spite of the efficient code you have written in the business logic for processing complex data !! One possible solution of handling all such scenarios is to update the UI of your Application from a separate thread. The thread would be responsible to update any UI on your forms and another thread would be processing the data simultaneously.I had this idea and when I tried to access windows controls from the newly created thread I had the exception which I have mentioned. I did some research on this and realized that “A control cannot be accessed from any other thread but the thread they were created on “. So for accessing UI elements from a different thread I have made a small framework which you can also use..Net provides a mechanism for this and it’s extremely simple. I think in the upcoming versions of .Net the background worker control would be more powerful and synchronization would be an included feature. So we don’t have to worry about any of it.For now I would just indicate how to update or access the UI from a different thread other than the main Application thread. My sample application is a VC# windows application compiled with .Net 2005 IDE and uses .Net 2 frameworks.I would just give you a step by step description instead of excerpts to make it more easy and understandable.The first thing we need to do is to create a System.Threading.Thread instance. It may be good to make it a form level variable since it might be accessed from different places on your form.ui.jpg

System.Threading.Thread  myThread;In the Constructor or any other suitable place initialize the Thread instance. The constructor is overloaded and I mostly use that takes the ThreadStart instance so I pass a new instance like: 

myThread = new Thread(new ThreadStart(ThreadProc));

 Note: Thread Start is a delegate and requires a target method which it points to and can be called once the thread is running.Now make a no argument method with the same name as you passed in the ThreadStart constructor:
public void ThreadProc()
{

   try
   {

        int count = 0;

        MethodInvoker mi;
        mi= MethodInvoker
(Updater);   

while (globalcount  < 300) 
  
{
         
     this.BeginInvoke(mi);

     Thread.Sleep(500);
         
  }
    
}

catch (Exception ex)    { // do nothing    }

}

We observe that we use another delegate “Method Invoker” which is the key basically. We make an instance of MethodInvoker delegate and pass a no argument method from which we access the UI of the form. After that we call the “BeginInvoke” of our main form and pass the instance of method Invoker.Note: As Method Invoker is also a delegate, we also need to specify the target method. In this case this would be the method from where UI is accessed. 

public void Updater()
{
   this.Text = globalcount.ToString();            
}

Once we are done with all of that all we have to do is to start the thread, so we would just call myThread.Start( ) ,and we are all set up.This way of updating the UI is also efficient in the sense that it is performed asynchronously instead of updating on timers or some other undesirable synchronous way.

 Want more sample code / applications feel free to contact me :) If you have some good design ideas, I would love to hear it!! also post ur comments if there is something specific u r looking for , thanks . If you need examples with C# or java where you wanna update your UI from a different threads I have the code available for you … fell free to discuss here or if this was of any use or not , i would appreciate your feedback !!

My email address is hiasad@hotmail.com

=) The most beautiful song

December 17, 2007

This is certainly one of my all time favourite songs , I like the Mark Chestnutt version way better than than Aerosmith’s Steven Tyeler :S hehehe he tries way 222222 hard and does some strange things with his fingers which my jaan finds so hilarious hahahaha !!! Anyways my jaan this is 4 u and it makes me think about yu !! And i Love yu !! heheheh =)

I could stay awake just to hear you breathing
Watch you smile while you are sleeping
While youre far away dreaming
I could spend my life in this sweet surrender
I could stay lost in this moment forever
Every moment spent with you is a moment I treasure

Dont want to close my eyes
I dont want to fall asleep
Cause Id miss you baby
And I dont want to miss a thing
Cause even when I dream of you
The sweetest dream will never do
Id still miss you baby
And I dont want to miss a thing

Lying close to you feeling your heart beating
And Im wondering what youre dreaming
Wondering if its me youre seeing
Then I kiss your eyes
And thank God were together
I just want to stay with you in this moment forever
Forever and ever

Dont want to close my eyes
I dont want to fall asleep
Cause Id miss you baby
And I dont want to miss a thing
Cause even when I dream of you
The sweetest dream will never do
Id still miss you baby
And I dont want to miss a thing

I dont want to miss one smile
I dont want to miss one kiss
I just want to be with you
Right here with you, just like this
I just want to hold you close
Feel your heart so close to mine
And just stay here in this moment
For all the rest of time

Dont want to close my eyes
I dont want to fall asleep
Cause Id miss you baby
And I dont want to miss a thing
Cause even when I dream of you
The sweetest dream will never do
Id still miss you baby
And I dont want to miss a thing