Queue and Thread

E

Eugene Anthony

First I leave the server running. Then I open up the client and click
the button to send a message to the queue. So when I check the queue its
empty. Possible because all the message in the queue has been read and
deleted. But I am interested to have my code reading one message from
the queue for every 'n' seconds. How is it done?.



Client.aspx.cs
-------------


using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Messaging;
using System.Threading;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
MessageQueue queue = new
MessageQueue(".\\Private$\\MyPrivateQueue");
queue.Send("Hello world");
}
}


Server.aspx.cs
--------------

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Messaging;
using System.Threading;

public partial class _Default : System.Web.UI.Page
{
Thread t;

protected void Page_Load(object sender, EventArgs e)
{
t = new Thread(new ThreadStart(readQueue));
t.Start();
}
protected void Button1_Click(object sender, EventArgs e)
{
MessageQueue queue = new
MessageQueue(".\\Private$\\MyPrivateQueue");
queue.Send("Hello world");
}

public void readQueue()
{
Thread.Sleep(10000);

try
{
MessageQueue queue = new
MessageQueue(".\\Private$\\MyPrivateQueue");
Message msg = queue.Receive(new TimeSpan(0, 0, 5));
msg.Formatter = new System.Messaging.XmlMessageFormatter(new
string[] { "System.String" });
Label1.Text = msg.Body.ToString();
}
catch (MessageQueueException)
{
Label1.Text = "There is no message in the queue";
}

readQueue();
}

protected void Button2_Click(object sender, EventArgs e)
{
try
{
MessageQueue queue = new
MessageQueue(".\\Private$\\MyPrivateQueue");
Message msg = queue.Receive(new TimeSpan(0,0,5));
msg.Formatter = new System.Messaging.XmlMessageFormatter(new
string[] { "System.String" });
Label1.Text = msg.Body.ToString();
}
catch (MessageQueueException)
{
Label1.Text = "There is no message in the queue";
}
}
}



Eugene Anthony
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top