myGoogleResults

Thursday, May 27, 2010

Messaging

MessageQueue myMQ;

public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
myMQ = new MessageQueue(@".\Private$\InteropQueue");

if (myMQ != null && !MessageQueue.Exists(myMQ.Path))
{
myMQ = MessageQueue.Create(@".\Private$\InteropQueue");

}

System.Messaging.Message msg = new System.Messaging.Message();
msg.Body = "Test";

myMQ.Send(msg);
}


MessageQueue mq;
System.Messaging.Message msg;
private void button2_Click(object sender, EventArgs e)
{
mq = new MessageQueue(@".\Private$\InteropQueue");

Type[] types = new Type[1];
types[0] = typeof(String);
mq.Formatter = new XmlMessageFormatter(types);

try
{
// Call receive on queue
msg = mq.Receive(new TimeSpan(0, 0, 100)); // 5 is wait time in seconds
// Assign to variable
MessageBox.Show(msg.Body.ToString());
}
catch( Exception ex)
{
MessageBox.Show(ex.Message);
}

No comments: