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 8377363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:39:16+00:00 2026-06-09T15:39:16+00:00

I have a need to start a new thread for a long running task

  • 0

I have a need to start a new thread for a long running task (writes to document store, triggering re-index etc).

I’m using Ninject in an ASP.Net MVC website. Essentially, I want to spawn a new thread from my Http Request, which instanciates it’s own objects, making sure that no database connections etc are used from the Http Request in the new thread. I’m not sure what to use to spawn the thread or how to configure Ninject to not re-use my database session from the web request context.

Below is a basic sequence diagram of the problem

I’ve read a bit about named scopes in Ninject but I’m not sure if in any way that’s related.

Ninject Thread Scope

The code for my CommandProcessor which will spawn the new thread is:

public class CommandProcessor
{
   private IKernel _serviceLocator;

   public CommandProc(IKernel serviceLocator)
   {
       _serviceLocator = serviceLocator;
   }

   public void Process<TCommand>(TCommand command) where T : ICommand
   {
       var commandHandlers = _serviceLocator.Get<ICommandHandler<TCommand>>();

       foreach (var commandHandler in commandHandlers)
       {
           // I want this call to be asynchronous
           commandHandler.Handle(command);
       }
   }
}

Thanks in advance,

Hamish.

  • 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-09T15:39:18+00:00Added an answer on June 9, 2026 at 3:39 pm

    When you spawn your new thread, create a new activation context from Ninject to use in the thread:

            Thread thread = new Thread(new ThreadStart(() =>
                {
                    using (IActivationBlock activation = kernel.BeginBlock())
                    {
                        var commandHandlers = activation.Get<ICommandHandler<TCommand>>();
    
                        foreach (var commandHandler in commandHandlers)
                        {
                            commandHandler.Handle(command);
                        }
                    }
                }));
            thread.Start();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a PyQt program, in this program I start a new thread for
I need to start NSTimer by 0:0:0 that I am doing and I have
I have a few servers and other daemons I need to start up in
I have some HUGE log files (50Mb; ~500K lines) I need to start filtering
Every now and again, I need to start the Django development server, and have
Here I have 2 requirements: Need a batch file to start a process on
I have start date and end date. I need to find out the day
I need change background of all text that have two spaces from the start
Very long story short, I have a need to place a WinForms Panel and
I have a broadcastreciever which start a long operation (uploading process). In the code

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.