Re: Relationship between Application.Exit() and AppDomain

Tech-Archive recommends: Fix windows errors by optimizing your registry



Sunny S wrote:
Hi Ben,

1. The Threads property of a Process instance should give you the set
of threads that are running in a given process or you can just use
Process.GetCurrentProcess.Threads.

2. Then for each thread in the ProcessThreadCollection returned by the
Threads property in (1) you can use the GetDomainID property to find
which AppDomains currently contain running threads and to map those
threads to individual AppDomains.

3. I am not sure if my speculations about your 3rd question are
correct, but I think that given the fact that AppDomains could be
unloaded and then recreated, even reused, I suspect, which is
certainly true for threads provided by the ThreadPool, associating an
AppDomain that started a particular thread with this thread or, the
other way round, associating a thread with an AppDomain that started
this thread might lead to confusion and/or could be costly. I doubt
that the runtime supports this. The only valid information would be
provided by an instant snapshot of which threads are running in which
AppDomains (see 2 above).

I was just trying to highlight the possibility that just because a thread is
currently running code in one AppDomain, doesn't mean it doesn't have other
AppDomains on the call stack and aborting the thread will interfere with
those other AppDomains. Or are cross-AppDomain calls always marshalled to a
thread dedicated to that AppDomain?

Consider for example the case where the plugin subscribed to an event in the
main program. When the main program fires the event handlers, the plugin
handler runs and hangs. How do you unload the AppDomain without throwing a
ThreadAbort exception up through the main program method which raised the
event?


Regards,

Sunny

"Ben Voigt [C++ MVP]" wrote:

"Jeffrey Tan[MSFT]" wrote:
Hi Willy,

Thank you for pointing out the document.

Yes, it seems that these issues are documented in MSDN clearly.
CannotUnloadAppDomainException is expected to throw in some
scenarios. Catching CannotUnloadAppDomainException and calling
AppDomain.Unload again should be the solution. Also, I think we'd
better give a guard to this. For example, we should check if this
will result in an infinite catch...re-calling loop. If so, I think
we have to kill the threads in the target AppDomain to recover from
this situation. Yes, this may not be an elegant solution, but we
have to defense this situation.

Is there any supported way to enumerate all Threads? All Threads
running code from a particular AppDomain? All Threads started by a
particular AppDomain? Nevermind the problems with threads ignoring
abort because they are in an I/O call or other native code.


Thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
=========================================
Delighting our customers is our #1 priority. We welcome your
comments and suggestions about how we can improve the support we
provide to you. Please feel free to let my manager know what you
think of the level of service provided. You can send feedback
directly to my manager at: msdnmg@xxxxxxxxxxxxxx

This posting is provided "AS IS" with no warranties, and confers no
rights.


.



Relevant Pages

  • Re: Relationship between Application.Exit() and AppDomain
    ... The Threads property of a Process instance should give you the set of ... Threads property in you can use the GetDomainID property to find which ... thread with an AppDomain that started this thread might lead to confusion ... Microsoft Online Community Support ...
    (microsoft.public.dotnet.framework.clr)
  • Re: AppDomains and static unmanaged variables
    ... wrapper assembly) to be marshaled over to a singleton AppDomain? ... Is there nothing to protect legacy code that does not ... get messed state management when running multiple web applications in the ... Microsoft MSDN Online Support Lead ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: How do I use AppDomains and add search paths to it?
    ... assembly that exists in another AppDomain. ... class and we are unwrapping this object in the default AppDomain, ... Microsoft Online Community Support ... where an initial response from the community or a Microsoft Support ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: New shim - what does use config file do?
    ... Let's look into the CreateAppDomain method, where the config file path is ... // Set the AppDomain to use a local DLL config if there is one. ... Microsoft Online Community Support ... nature are best handled working with a dedicated Microsoft Support Engineer ...
    (microsoft.public.office.developer.com.add_ins)
  • RE: Further to the Discussion
    ... correctly for the 'Relationship between Application.Exitand AppDomain' ... Since you are writing the .Net Winform code, ... Microsoft Online Community Support ... where an initial response from the community or a Microsoft Support ...
    (microsoft.public.dotnet.framework.clr)