Thread Unwounding Problems
-
Scenario - I have a web application in ASP.NET and C# which has a backend as MSMQ. I have a private Microsoft Queue (MSMQ) which has to be constantly read for any contents that are submitted to it by users via webpage. As soon as there is something in the queue, it has to be read and processed. For this I have a thread called QRead in Global.asax. It refers to a static method ReadQ that is in an infinte while loop. the problem is that the application behaves randomly since the thread QRead in continously monitoring the queue and is always active. The CPU usage goes to 100% after the first time you launch tha application. QRead thread cannot be put to sleep since we need it to monitor the queue. I also get a popup sometimes that say "Application failed to start poperly since the thread cannot be unwound out of it. CC92. No source code available." Can some one please help !!! Code is as below ...... Global.asax ------------------------ protected void Application_Start(Object sender, EventArgs e) { Thread QRead = new Thread(new ThreadStart(SthQueueHandler.ReadQ)); QRead.IsBackground = false; QRead.Start(); } ------------------------ ReadQ ------------------------ public static void ReadQ() { while(true) { // CODE TO PROCESS QUEUE CONTENTS } } ------------------------- A student knows little about a lot. A professor knows a lot about little. I know everything about nothing.
-
Scenario - I have a web application in ASP.NET and C# which has a backend as MSMQ. I have a private Microsoft Queue (MSMQ) which has to be constantly read for any contents that are submitted to it by users via webpage. As soon as there is something in the queue, it has to be read and processed. For this I have a thread called QRead in Global.asax. It refers to a static method ReadQ that is in an infinte while loop. the problem is that the application behaves randomly since the thread QRead in continously monitoring the queue and is always active. The CPU usage goes to 100% after the first time you launch tha application. QRead thread cannot be put to sleep since we need it to monitor the queue. I also get a popup sometimes that say "Application failed to start poperly since the thread cannot be unwound out of it. CC92. No source code available." Can some one please help !!! Code is as below ...... Global.asax ------------------------ protected void Application_Start(Object sender, EventArgs e) { Thread QRead = new Thread(new ThreadStart(SthQueueHandler.ReadQ)); QRead.IsBackground = false; QRead.Start(); } ------------------------ ReadQ ------------------------ public static void ReadQ() { while(true) { // CODE TO PROCESS QUEUE CONTENTS } } ------------------------- A student knows little about a lot. A professor knows a lot about little. I know everything about nothing.
First of all, having a thread running in an infite loop in a web application is just poor design. Why do you need to have this thread running? You do know that this thread is taken from the thread pool for the application, which is the same place where request threads come from? What are you trying to read from the queue? Can't you just do it on the Begin_Request event handler for the application? Seems a little better than just an infinite loop. ~Javier Lozano (blog)
-
Scenario - I have a web application in ASP.NET and C# which has a backend as MSMQ. I have a private Microsoft Queue (MSMQ) which has to be constantly read for any contents that are submitted to it by users via webpage. As soon as there is something in the queue, it has to be read and processed. For this I have a thread called QRead in Global.asax. It refers to a static method ReadQ that is in an infinte while loop. the problem is that the application behaves randomly since the thread QRead in continously monitoring the queue and is always active. The CPU usage goes to 100% after the first time you launch tha application. QRead thread cannot be put to sleep since we need it to monitor the queue. I also get a popup sometimes that say "Application failed to start poperly since the thread cannot be unwound out of it. CC92. No source code available." Can some one please help !!! Code is as below ...... Global.asax ------------------------ protected void Application_Start(Object sender, EventArgs e) { Thread QRead = new Thread(new ThreadStart(SthQueueHandler.ReadQ)); QRead.IsBackground = false; QRead.Start(); } ------------------------ ReadQ ------------------------ public static void ReadQ() { while(true) { // CODE TO PROCESS QUEUE CONTENTS } } ------------------------- A student knows little about a lot. A professor knows a lot about little. I know everything about nothing.
This is not the way to read from a MessageQueue. Read up on MessgaeQueue.BeginReceive and handling the the event with an eventhandler of type MessageQueue.ReceiveCompleted. Basically, you put all your reading logic in the event handler. Then you make a call to .BeginReceive in your main startup code. When the queue receives a message, your handler will be called, then you end the handler with another call to .BeginReceive. Here is some code:
static void MyStart()
{
Type[] AllowedTypes = {Type.GetType("MyNamespace.Type")};//Create queue object q = new MessageQueue("queuePath"); //Initialize que to handle your message objects q.Formatter = new XmlMessageFormatter(AllowedTypes); //Set up event handler q.ReceiveCompleted += new ReceiveCompletedEventHandler(q\_ReceiveCompleted); //Start receiving messages q.BeginReceive();
}
private static void q_ReceiveCompleted(object sender, ReceiveCompletedEventArgs e)
{
//Put code here to read from the queue
//...
//...
//Now wait for another message
q.BeginReceive();
}