Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. MSMQ Error Handling

MSMQ Error Handling

Scheduled Pinned Locked Moved C#
help
11 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    ramdil
    wrote on last edited by
    #1

    Hi all I am new to MSMQ.I have an application which sends a message.This message will be recieved by another application and stores in a MSMQ.And then it will read from the msmq and sends to some other process.Now the thing i am facing is that i have a recieve thread which starts at the application_start event .inside this thread i am calling the receive method of msmq.and i am using the follwing code with timespan msg = mq.Receive(new TimeSpan(0,0,100)); now i know after 100 ms it will show MessageQueueException.my doubt is that what should i do after i catch the exception,because i want the thread to abort only at the application_end.so what should i do if a exception occurs. Thanks in advance.

    Regards DilipRam

    J 1 Reply Last reply
    0
    • R ramdil

      Hi all I am new to MSMQ.I have an application which sends a message.This message will be recieved by another application and stores in a MSMQ.And then it will read from the msmq and sends to some other process.Now the thing i am facing is that i have a recieve thread which starts at the application_start event .inside this thread i am calling the receive method of msmq.and i am using the follwing code with timespan msg = mq.Receive(new TimeSpan(0,0,100)); now i know after 100 ms it will show MessageQueueException.my doubt is that what should i do after i catch the exception,because i want the thread to abort only at the application_end.so what should i do if a exception occurs. Thanks in advance.

      Regards DilipRam

      J Offline
      J Offline
      Jimmanuel
      wrote on last edited by
      #2

      ramdil wrote:

      what should i do if a exception

      What do you want your app to do? wait for another message? do nothing until the app exits?

      R 1 Reply Last reply
      0
      • J Jimmanuel

        ramdil wrote:

        what should i do if a exception

        What do you want your app to do? wait for another message? do nothing until the app exits?

        R Offline
        R Offline
        ramdil
        wrote on last edited by
        #3

        Yes it should wait for message to arrive. actually since i have started my thread on application , then the recieve method will get time out after the span of time ,it will throw messaging exception..so my doubt is in this exception section, i again need to wait until message comes

        Regards DilipRam

        J 1 Reply Last reply
        0
        • R ramdil

          Yes it should wait for message to arrive. actually since i have started my thread on application , then the recieve method will get time out after the span of time ,it will throw messaging exception..so my doubt is in this exception section, i again need to wait until message comes

          Regards DilipRam

          J Offline
          J Offline
          Jimmanuel
          wrote on last edited by
          #4

          so catch the exception inside of a while loop:

          while (listenForMessages)
          {
          try
          {
          msg = mq.Receive(new TimeSpan(0,0,100));

              // parse the message here . . .
          
              // if you don't want to listen for more messages then set
              listenForMessages = false;
          }
          catch (MessageQueueException ex)
          {
              // log the error somewhere
          }
          

          }

          R 1 Reply Last reply
          0
          • J Jimmanuel

            so catch the exception inside of a while loop:

            while (listenForMessages)
            {
            try
            {
            msg = mq.Receive(new TimeSpan(0,0,100));

                // parse the message here . . .
            
                // if you don't want to listen for more messages then set
                listenForMessages = false;
            }
            catch (MessageQueueException ex)
            {
                // log the error somewhere
            }
            

            }

            R Offline
            R Offline
            ramdil
            wrote on last edited by
            #5

            Thanks for the reply. i dont want to catch the exception.i want it to listen again..so what should i do..if i move a thread to sleep(100) in exception, will it serve my purpose. i am pasting my code section. while (IsMessageRecieved) { try { mq = new MessageQueue(XMLMSMQRecievePath); msg = mq.Receive(new TimeSpan(0,0,100)); XMLMSMQFileName = msg.Body.ToString(); } catch (System.Messaging.MessageQueueException ex) { Logging.TraceError(ex.Message); mq.Purge(); Thread.Sleep(100); }

            Regards DilipRam

            J 1 Reply Last reply
            0
            • R ramdil

              Thanks for the reply. i dont want to catch the exception.i want it to listen again..so what should i do..if i move a thread to sleep(100) in exception, will it serve my purpose. i am pasting my code section. while (IsMessageRecieved) { try { mq = new MessageQueue(XMLMSMQRecievePath); msg = mq.Receive(new TimeSpan(0,0,100)); XMLMSMQFileName = msg.Body.ToString(); } catch (System.Messaging.MessageQueueException ex) { Logging.TraceError(ex.Message); mq.Purge(); Thread.Sleep(100); }

              Regards DilipRam

              J Offline
              J Offline
              Jimmanuel
              wrote on last edited by
              #6

              I'm not quite sure that I fully understand you. If you don't catch the exception then the program will explode. From the code segment posted, it looks like you want to read the first available message from the MSMQ and store the body of it in the variable "XMLMSMQFileName" and for each 100 ms that no message is received log an error . . . is this correct? If so there are a few things to note: 1) there's no need to set mq to a new MessageQueue in each iteration of the loop. setting it once before the loop should be sufficient 2) if the timeout expires and the exception is thrown that means there are no messages in the queue, so calling Purge() is unnecessary 3) IsMessageRecieved needs to be set to true or else it will keep reading messages from the queue

              R 1 Reply Last reply
              0
              • J Jimmanuel

                I'm not quite sure that I fully understand you. If you don't catch the exception then the program will explode. From the code segment posted, it looks like you want to read the first available message from the MSMQ and store the body of it in the variable "XMLMSMQFileName" and for each 100 ms that no message is received log an error . . . is this correct? If so there are a few things to note: 1) there's no need to set mq to a new MessageQueue in each iteration of the loop. setting it once before the loop should be sufficient 2) if the timeout expires and the exception is thrown that means there are no messages in the queue, so calling Purge() is unnecessary 3) IsMessageRecieved needs to be set to true or else it will keep reading messages from the queue

                R Offline
                R Offline
                ramdil
                wrote on last edited by
                #7

                Thanks for the reply.yes..it will wait for 100ms then not will go to exception.but i dont want to set the flag value to change,because what i want is that if the error occurs, then it should again wait unitl message comes.. so i am asking that if i am adding thread.sleep(100) will serve my purpose ..that means what actually i want is that on exception i dont want to exit the loop but should wait for the mssage to arrive.. will my code will satisfy if do sleep(100).hope i am clear with my requirement Thanks in advance

                Regards DilipRam

                J 1 Reply Last reply
                0
                • R ramdil

                  Thanks for the reply.yes..it will wait for 100ms then not will go to exception.but i dont want to set the flag value to change,because what i want is that if the error occurs, then it should again wait unitl message comes.. so i am asking that if i am adding thread.sleep(100) will serve my purpose ..that means what actually i want is that on exception i dont want to exit the loop but should wait for the mssage to arrive.. will my code will satisfy if do sleep(100).hope i am clear with my requirement Thanks in advance

                  Regards DilipRam

                  J Offline
                  J Offline
                  Jimmanuel
                  wrote on last edited by
                  #8

                  no, Thread.Sleep won't do what you want, all it'll accomplish is to make the Thread wait for 100 more ms before it executes the next iteration of the loop. The way that it is currently written it will accomplish what you're describing, but you still need to add the line "IsMessageRecieved = false;" into the try block after the message has successfully been read like this:

                  try
                  {
                  msg = mq.Receive(new TimeSpan(0,0,100));
                  XMLMSMQFileName = msg.Body.ToString();
                  IsMessageRecieved = false;
                  }
                  catch . . .

                  this will make it so that if a message is received successfully then IsMessageRecieved will be set to false and the while loop will be exited but if the Receive method times out then the line IsMessageRecieved = false; is skipped and the loop is repeated


                  Last modified: 10mins after originally posted -- corrected IsMessageRecieved = true; to IsMessageRecieved = false;

                  R 1 Reply Last reply
                  0
                  • J Jimmanuel

                    no, Thread.Sleep won't do what you want, all it'll accomplish is to make the Thread wait for 100 more ms before it executes the next iteration of the loop. The way that it is currently written it will accomplish what you're describing, but you still need to add the line "IsMessageRecieved = false;" into the try block after the message has successfully been read like this:

                    try
                    {
                    msg = mq.Receive(new TimeSpan(0,0,100));
                    XMLMSMQFileName = msg.Body.ToString();
                    IsMessageRecieved = false;
                    }
                    catch . . .

                    this will make it so that if a message is received successfully then IsMessageRecieved will be set to false and the while loop will be exited but if the Receive method times out then the line IsMessageRecieved = false; is skipped and the loop is repeated


                    Last modified: 10mins after originally posted -- corrected IsMessageRecieved = true; to IsMessageRecieved = false;

                    R Offline
                    R Offline
                    ramdil
                    wrote on last edited by
                    #9

                    Hi first of all thanks for detailed description,i understand what you are telling me but the think here is that i calling this method in a thread.thread starts at appn_start and thread stops at appn_end and here only i setting the variable value IsMessageRecieved=false..so that when ever my application is running mode, it will always check whether any message is in msmq then it will read..if i am setting flag variable inside the iteration, then once it is read then it will not listen...hope u understand my requriment.in this context ,will my code work with out any problem..this is my doubt

                    Regards DilipRam

                    J 1 Reply Last reply
                    0
                    • R ramdil

                      Hi first of all thanks for detailed description,i understand what you are telling me but the think here is that i calling this method in a thread.thread starts at appn_start and thread stops at appn_end and here only i setting the variable value IsMessageRecieved=false..so that when ever my application is running mode, it will always check whether any message is in msmq then it will read..if i am setting flag variable inside the iteration, then once it is read then it will not listen...hope u understand my requriment.in this context ,will my code work with out any problem..this is my doubt

                      Regards DilipRam

                      J Offline
                      J Offline
                      Jimmanuel
                      wrote on last edited by
                      #10

                      You're absolutely correct! I was thinking that you were reading only one message; the way you've described it sounds like you have it set up correctly to continue reading messages until the application ends, so it should work fine. Just like before though, it's probably a good idea to set the IsBackground property to true on the Thread object so that it doesn't hang up you application when it's closing. Things should work fine even if you don't do this, but it probably won't hurt to throw this in just in case. :)

                      R 1 Reply Last reply
                      0
                      • J Jimmanuel

                        You're absolutely correct! I was thinking that you were reading only one message; the way you've described it sounds like you have it set up correctly to continue reading messages until the application ends, so it should work fine. Just like before though, it's probably a good idea to set the IsBackground property to true on the Thread object so that it doesn't hang up you application when it's closing. Things should work fine even if you don't do this, but it probably won't hurt to throw this in just in case. :)

                        R Offline
                        R Offline
                        ramdil
                        wrote on last edited by
                        #11

                        Thanks for the reply

                        Regards DilipRam

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups