.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

AppDomain Unload does corrupt Thread Pool

Posted By:      Posted Date: September 10, 2010    Points: 0   Category :.NET Framework
Hi,I was just trying to run a simple scenario:1. Create AppDomain2. Use up all Worker threads from Thread Pool 3. Unload AppDomain before the worker threads have finished.4. Try to queue another WorkItem in your default or some other AppDomainResult: The .NET thread pool is dead because there are no threads left for the other AppDomain. ThreadPool Threads are not given back to the thread pool when an AppDomain.Unload is performed. I think it is ok to not kill the threads (reliability) but what is not ok is that there are no threads left for other AppDomains. An AppDomain unload should increase the number of worker/io completion port threads which are currently running inside the to be unloaded AppDomain to ensure that you do not run out of threads after 2-3 days of regular operation when some AppDomains where created and destroyed in the meantime.This will eat up your thread pool quite fast.Sample Code to reproduce:using System;using System.Collections.Generic;using System.Text;using System.Threading;using System.Diagnostics;namespace ThreadPoolAndAppDomain{    [Serializable]    public class Worker    {       public Worker()       {}       int nQueuedItems = 0;              public void QueueWorkItems(int n)       {           for (int i = 0; i < n; i++)           {               ThreadPool.QueueUserWorkItem(delegate(object state)               {                   lock (this) 

View Complete Post

More Related Resource Links

Pooled Threads: Improve Scalability With New Thread Pool APIs


The new threadpool APIs in Windows Vista and in the upcoming Windows Server 2008 provide advances that even native Windows developers can benefit from.

Robert Saccone

MSDN Magazine October 2007

.NET Column: The CLR's Thread Pool


How does the thread pool work in the CLR?

Jeffrey Richter

MSDN Magazine June 2003

AppDomain.Unload effects Unable to evaluate expression because the code is optimized or a native fra

Hello, I create a custom AppDomain.Create().  I registered the Unhandled Exception Event from my custom AppDomain. In the Unhandled Exception Event i try to Unload my AppDomain with AppDomain.Unload. But this effects a "Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack." Have anyone a idea what i can do against this behaviour. greetints

Assembly is not unloaded on AppDomain unload


In order to attempt to unload an assembly I use the following code. Yet the assembly is not unloaded. The second call to Execute is performed yet it shouldn't be available anymore should it?

Also the HandlerOne.dll is still locked. As I load it from memory I wonder why it is still locked


    // Copy assembly in memory to prevent file lock
    System.IO.StreamReader reader;
    byte[] buf;
    using ( reader = new System.IO.StreamReader( "HandlerOne.dll", System.Text.Encoding.GetEncoding( 1252 ), false ) )
      buf = new byte[reader.BaseStream.Length];
      reader.BaseStream.Read( buf, 0, System.Convert.ToInt32( reader.BaseStream.Length ) );

    // Create a seperate AppDomain
    // Load the handler
    AppDomain d = AppDomain.CreateDomain( "one" );
    Assembly a = d.Load( buf );
    Type typ = a.GetType( "HandlerLib.HandlerOne", <

AppDomain.Unload called multiple times?




     In a simple console app, I created a separate AppDomain (name: ad2)  to do some work.  In this AppDomain, I created a separate worker thread  and deliberately put the thread to sleep for long time (2 mins) in the Finally block.


      I also subscribed to the ad2’s Unload event  to do some logging.


      Immediately after calling the worker method, I called the AppDomain.Unload(ad2) in the main thread.  Because of the long Thread.Sleep() in the worker thread finally block, a CannotUnloadAppdomainException was thrown when calling AppDomain.Unload(ad2).

Unable to unload new appdomain completly


Hello friends,

I am using VS2008 with C#. I have an issue related to reflection. In my application I have a dropdown with 2 options. if user selects option1 assembly1 is loaded and invoke a method of loaded assembly. If the user selects option2, assembly2 gets loaded and a method of loaded assembly is invoked. now the issue is both the assemblies have most of the classes common. When the user selects option1 assembly1 loads and invoke a method. this method sets some static object. Now when user selects option2 it loads assembly2 and when that method is invoked it throws an exception due to the static method. I am not suppos to change the code of the assembly namespace. So the solution should be that the assembly should be unloaded after the method has been invoked. But it is impossible to unload an assembly once loaded.

My Findings. By searching on the net what I have learned is I have only one option that create a new appdomain and load the assembly in that appdomain and after the method invoke unload the assembly. i have implemented that but still I am getting the same issue. My guess is that I am not properly unloading the appdomain. i am writing  down the code that I am using. Please have a look and let me know if I am making any mistake while operating with appdomains.

AppDomainSetup setup = AppDomain.CurrentDomain.SetupInformation;


Singleton Thread Pool



The thread pool that remoting infrastructure use to process simultaneous client's requests is the same thread pool of the host process?

If it is the same pool, threads i take to do normal work on the remote object decrease the throughput of the application beacause remoting infrastructure would have less threads to handle client's requests.


Thanks in advance.

Failed to create AppDomain


Recently updated my system to use Visual Studio 2010, and projects will no longer compile. All of them give the error "Failed to create AppDomain." Intellisense does not work either (except for standard HTML tags), and individual pages will also not compile.

Points of note:

  • All sites are hosted on UNC shares (although it doesn't work on a new site created on local drive either)
  • Language is mostly C#
  • No explicit code is creating AppDomain objects
  • Sites compiled fine under both VS 2005 and 2008 both before and after VS 2010 installation
  • Sites compile and run fine from the IIS server on the remote machines (one IIS 6, one IIS 7)

Things I've tried (some obviously shouldn't have any bearing on it, but I've tried just about everything that anyone suggested on any forum I could find, most of which seemed to apply to IIS, not VS):

  • renaming/deleting security.config
  • running caspol -reset
  • re-registering asp.net: aspnet_regiis.exe -ua / aspnet_regiis.exe -i
  • removing VS 2005, 2008, 2010, and then re-installing just VS 2010
  • adding the UNC path to the local intranet zone (internet options control panel)
  • adding the UNC path to LocalIntranet_Zone in the ASP.Net 2.0 control panel (runtime security policy/machine/al

Thread Performance: Resource Contention Concurrency Profiling in Visual Studio 2010


Visual Studio 2010 includes new resource contention profiling features that help detect concurrency contention among threads. We walk through a profiling investigation to demonstrate how you can pinpoint and fix resource contention problems in your code.

Maxim Goldin

MSDN Magazine June 2010

Thread Diagnostics: Performance Tuning with The Concurrency Visualizer in Visual Studio 2010


Understanding performance bottlenecks in multithreaded apps used to require significant instrumentation and analysis by expert developers. The Concurrency Visualizer in Visual Studio 2010 significantly reduces the burden of parallel performance analysis.

Hazim Shafi

MSDN Magazine March 2010

CLR Inside Out: Thread Management In The CLR


Getting the performance you want in concurrent applications is not as straightforward as you might think. See how common threading issues can affect your application.

Erika Fuentes and Eric Eilebrecht

MSDN Magazine December 2008

Editor's Note: new Thread(ReadEditorsNote).Start(); yourAttention.WaitOne();


Introducing the issue, Howard Dierking points out that you can't simply parallelize your code blindly if you expect to truly reap the benefits that parallelism promises.

Howard Dierking

MSDN Magazine October 2008

Concurrent Affairs: Build a Richer Thread Synchronization Lock


In my last column, I showed the various thread synchronization mechanisms employed by the Microsoft® . NET Framework (see Concurrent Affairs: Performance-Conscious Thread Synchronization). I then examined the performance characteristics of all these mechanisms and determined that the Interlocked methods performed the best because the calling thread never has to transition to kernel mode.

Jeffrey Richter

MSDN Magazine March 2006

Concurrent Affairs: Performance-Conscious Thread Synchronization


In my career, I have architected and implemented many thread synchronization techniques. This has provided me with a lot of experience that has shaped the way I now think about thread synchronization problems.

Jeffrey Richter

MSDN Magazine October 2005

Basic Instincts: Thread Synchronization


My last three Basic Instincts columns have examined techniques for using asynchronous delegates and creating secondary threads. Those columns demonstrated how to introduce multithreaded behavior into your applications.

Ted Pattison

MSDN Magazine September 2004

ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend