Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8071991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T13:52:08+00:00 2026-06-05T13:52:08+00:00

I wrote this code which is working but it made me start thinking how

  • 0

I wrote this code which is working but it made me start thinking how should I use MSMQ:
Should I use a “using” statement to get the message queue? the Using statement will handle getting rid of the queue when I am done with it, but when am I done with it? Should I save my MessageQueue object for reuse. Lets say this is in an ASP.Net application and I want to send 3 messages to the same queue from the same action. Save the object and reuse or create and dispose as I am right now?

using (MessageQueue msgQueue = new MessageQueue(this.queueName))
{
    using (MessageQueueTransaction msgTx = new MessageQueueTransaction())
    {                      
        Message recoverableMessage = null;
        msgTx.Begin();
        try
        {
            recoverableMessage = new Message();
            recoverableMessage.Body = message;
            recoverableMessage.Formatter = new BinaryMessageFormatter(System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple, System.Runtime.Serialization.Formatters.FormatterTypeStyle.TypesAlways);
            recoverableMessage.Recoverable = true;

            msgQueue.Send(recoverableMessage, msgTx);
            ////this.msgQ.Send(recoverableMessage, msgTx); //whats up here??? this will teach me for taking time off. not sure whuc I shuold use
            msgTx.Commit();
            result = recoverableMessage.Id;
            //recoverableMessage.Dispose();
        }
        catch (Exception)
        {
            msgTx.Abort();
            ////recoverableMessage.Dispose();
        }
        finally
        {
            if (recoverableMessage != null)
            {
                recoverableMessage.Dispose();
            }
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T13:52:10+00:00Added an answer on June 5, 2026 at 1:52 pm

    In general, you should keep objects alive as short as possible. This is to prevent .NET garbage collector from moving those objects from generation 0 to generation 1 and so on. This will help to reduce memory consumption & number of garbage collections.

    Having said that, the only reason to keep a message queue object alive would be to post multiple messages to the same queue as part of a single transaction or scope.

    If the three messages are sent during a single page request, then you create a new instance of the message queue, send the messages and dispose of the queue each time a page is rendered.

    EDIT
    For details on thread satefy etc. you should refer to the documentation.
    For example, MSDN says that only the GetAllMessages method is thread safe. I would then assume that the rest of the API isn’t thread safe. However, this only means that a single instance of MessageQueue object should not be accessed concurrently by multiple threads. You can create multiple instances of MessageQueue pointing to the same queue fine.

    Regarding performance, I think you may be trying to optimise prematurely. I suggest profiling your software (only the release build) and putting it under some load. That’s the only way you’ll get a reliable answer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote this code. The constructor works normally, but in the destructor I get
I wrote a code using this tutorial http://marakana.com/forums/android/examples/311.html It is working fine except the
I wrote this little code std::map<int,template<class T>> map_; map_.insert(make_pair<int,message>(myMsg.id,myMsg)); but the compiler doesn't seem
I wrote this simple C code and compiled it using Visual Studio 2010, with
I wrote this code in C# to encrypt a string with a key: private
I wrote this code that compiles on Solaris gcc it works fine too for
I wrote this code in my viewDidLoad function in my iOS project. lettersLeftLabel.text =
I wrote this code: ScheduledExecutorService ExtractorTimer=Executors.newScheduledThreadPool(1); final ScheduledFuture<?> SchedulerHandle; SchedulerHandle =ExtractorTimer.scheduleWithFixedDelay( new Runnable() {
I wrote this tiny code: #include <stdio.h> int main() { size_t temp; temp =
I wrote this piece of code that is supposed to redirect something written on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.