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

  • SEARCH
  • Home
  • 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 7672727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:21:11+00:00 2026-05-31T16:21:11+00:00

I have a class that saves logging information to the database (in an NServiceBus

  • 0

I have a class that saves logging information to the database (in an NServiceBus message Handle method).

Most of the time that logging can be done on a separate thread (and transaction) from the main process. But it all needs to be done on the same background thread (meaning that they have to be done in order, just not in sync with the main process).

However, once it starts Foreign Keying to the actual data from NServiceBus, it needs to be on the same thread.

Here is some example code:

public class MyExample
{

   public void LogSomeStuff(Stuff stuff)
   {
      using (MoveOutsideTransaction())
      {
         // Do Method Stuff here
         dataAccess.SaveChanges();
      }
   }


   public void LogSomeOtherStuff(Stuff stuff)
   {
      using (MoveOutsideTransaction())
      {
         // Do Other Method Stuff here
         dataAccess.SaveChanges();
      }
   }

   private IDisposable MoveOutsideTransaction()
   {
       if (loggingOutsideTransaction)
           return new TransactionScope(TransactionScopeOption.Suppress);

       return null;
   }
}

I am wondering if there is a way to use my transaction conditional to also conditionally move the running to a different thread. (But only when it suppresses the transaction.)

  • 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-05-31T16:21:12+00:00Added an answer on May 31, 2026 at 4:21 pm

    I am wondering if there is a way to use my transaction conditional to
    also conditionally move the running to a different thread.

    You cannot just move the execution of a method onto a different thread. Threads do not work that way in general. However, what you can do is setup a dedicated thread that can participate in a marshaling operation which does, more or less anyway, simulate the transfer of execution to another thread. It is important to drive home the point that this dedicated thread must be specially coded to accept this marshaling operation.

    Here is how it works.

    public class DedicatedThread
    {
      private BlockingCollection<Action> actions = new BlockingCollection<Action>();
    
      public DedicatedThread()
      {
        var thread = new Thread(
          () =>
          {
            while (true)
            {
              Action action = actions.Take();
              action();
            }
          });
      }
    
      public void SubmitAction(Action action)
      {
        actions.Add(action);
      }
    }
    

    And you might use it like this.

    if (loggingOutsideTransaction) 
    {  
      // Execute asynchronously on a dedicated thread.
      dedicatedThread.SubmitAction(yourActionDelegate); 
    }
    else
    {
      // Execute synchronously.
      yourActionDelegate();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a CoreDataUtilities class with a class method that saves a managed object
for a Java project for a University Class I have a method that saves
Currently I have a static class that I use as my logging module. I’ve
I have a report class that saves data like summary page views, banner impressions
I have made a .NET class library in C# that initializes some logging, sent
I have a Manager class that saves data in the SQL table and also
I have a class that I instantiate to save or load xml data. For
I have a Class A that when it is instantiated and saved for the
I have a fairly simple class that I want to save to SQL Server
I have class that extend FragmentActivity in it I add fragment to layout as

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.