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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:01:01+00:00 2026-06-07T11:01:01+00:00

I read on MSDN that the difference between a foreground thread and a background

  • 0

I read on MSDN that the difference between a foreground thread and a background thread is that an application can’t terminate until all of its foreground threads have terminated, whereas it won’t bother waiting for background threads. I decided to try this out just to get a better understanding of threads.

Thread t = new Thread(Work);  //Work() just has the thread sleep for a long time
t.IsBackground = false; //make it a foreground thread
t.Start();
while(true)
{
    if(Session["checker"] != null)
    {
        Session["checker"] = true;
        System.Diagnostics.Debug.Write("I'm here!");
    }
}

I used a session variable to know if the AppDomain has been restarted since sessions get cleared upon an AppDomain restart.

So when I save the web.config file, it should trigger the AppDomain to restart, which should require it to wait for my long running thread t since t is running in the foreground. However when I touch the web.config file, goes straight into clearing my Session["checker"] and printing out “I’m here!”, so I know that my application didn’t wait for my thread.

Am I misunderstanding how foreground threads are supposed to work? Shouldn’t the AppDomain restart wait for my thread to finish executing before it goes and starts clearing my session variables?

Thanks

  • 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-07T11:01:02+00:00Added an answer on June 7, 2026 at 11:01 am

    Please, pay attention to the word “application” in statement “application can’t terminate until all of its foreground threads have terminated”. In case of recycling, application is not terminated. During recycling ASP.NET is unloading old AppDomain and loading new one. Everything is done inside single process. Process isn’t terminated. In scope of AppDomain unloading, all threads (Background and Foreground) are killed.
    So, ASP.NET won’t wait for foreground threads to complete during recycling.
    Try simple console application which creates a Foreground thread in its Main method. It will work until the thread is terminated.

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

Sidebar

Related Questions

I've read about advantages of Tasks Difference between Task (System.Threading.Task) and Thread Also msdn
Once I read an MSDN article that encouraged the following programming paradigm (its not
I have read on MSDN( see Important note ) that RSACryptoServiceProvider must be disposed.
I read the MSDN documentation and examples here and I know that the correct
I've read the MSDN article about the layouts pass, that states: When a node
I am not sure that I understand Isolated storage. I read the article http://msdn.microsoft.com/en-us/library/3ak841sy%28VS.80%29.aspx
Can you please describe an example indicating difference between Public and Private Cache-Control in
I have read about executing asynchronous operations and i found that it can be
So I read MSDN and Stack Overflow. I understand what the Action Delegate does
I read the msdn article about disposing objects in http://msdn.microsoft.com/en-us/library/ee557362(office.14).aspx now I'm really confused

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.