1

I need to have an event fired whenever any AppDomain unloads - including the default one of the process. The problem with AppDomain.DomainUnload is that it only fires for non-default AppDomains. Furthermore, AppDomain.ProcessExit has limited execution time, which I cannot rely on.

Any suggestions as to how I can achieve this would be greatly appreciated!

(Alternatively, having an event fired when a background thread (Thread.IsBackground == True) works too.)

tshepang
  • 12,111
  • 21
  • 91
  • 136
  • What are you trying to catch? Are you using ASP.Net, Winforms, WPF or console? In an App, a DLL or a service? – Spence Dec 29 '10 at 05:23
  • A plain console app. I simply want to have an event fired when the default AppDomain exits (i.e. when Main ends). –  Dec 30 '10 at 11:39
  • 5
    Why do you not put your code in the end of Main? – jgauffin Jan 02 '11 at 12:22
  • 3
    If you are going to handle something that runs after the default app domain in unloaded..which appdomain do you think that code will run in? I could be wrong but to me it is like getting a chicken to lay an egg when it no longer alive.. :D I think .net allows only visibility of app domains loaded by the host process... – Mahesh Jan 03 '11 at 10:55
  • Ideally, the event would fire _just before_ the domain is unloaded. :) –  Jan 03 '11 at 10:56
  • @Zor, in that case you could prevent the appdomain from ever being unloaded if the event handler runs inside that appdomain. –  Jan 07 '11 at 10:01

4 Answers4

1

Why do you not put your code in the end of Main?

As for an event:

No there is no event which will get executed when the application domain is unloaded. If you are a library developer you add a destructor to your entry class. But beware that everything might not be collected properly. Read this answer: https://stackoverflow.com/a/2735431/70386

Community
  • 1
  • 1
jgauffin
  • 99,844
  • 45
  • 235
  • 372
  • 2
    If it's a framework this is more like a documentation issue. Tell your users do invoke your `Cleanup()` method or whatever it's called when the application is shut down. – jgauffin Jan 06 '11 at 09:12
  • Ditto to @jgauffin's comment. Better yet, have an IDisposable class so the caller is responsible for deterministic disposal. – TrueWill Jan 07 '11 at 18:00
  • I suppose I could make some sort of disposable context for the system, but it felt more appropriate to make it static, as it works somewhat like the ThreadPool (but in this case, specialized for actors). –  Jan 07 '11 at 23:50
  • I ended up going with that approach. It's not exactly what I'd like, but nevertheless, does what I need in a manner that consumers can reason about. Thanks for the input, folks! –  Jan 08 '11 at 15:15
  • The problem with updating the documentation to tell users to invoke `Cleanup()` method, is that users are required to invoke a `Cleanup()` method. i prefer the class automatically handle the tasks required when the AppDomain is ending (e.g. save things to database). If users are required to call a method, then they simply won't call it. Hell, **i'm** the author of the framwork, and the person who will use it, and **i** don't want to call it. – Ian Boyd Aug 02 '13 at 20:14
  • @IanBoyd: So you vote -1 because the OPs chosen solution fit your need? Ask a new specific question as this answer DID answer the OPs question. Or as you say: ***You*** have another **similar** problem. ***You*** should then ask a new question. – jgauffin Aug 02 '13 at 21:08
  • i downvoted because it didn't answer OPs question. i have the *exact* same question. Question: *"Is there an unload event for the default AppDomain?" Answer: *"Why do you not put your code in the end of Main?"* Also, it looks like the questioner attached a bounty, so the (only) upvoted answer is auto-accepted; whether it answered the question or not. i created [a duplicate of this question, that doesn't allow weaseling out of answering the question](http://stackoverflow.com/questions/18033100/unload-event-for-the-default-application-domain) – Ian Boyd Aug 03 '13 at 12:57
  • Why do you think that he accepted my answer and awarded me the bounty if he did not think that it was the correct answer? Bounties are not automatically marking questions as accepted. They can be awarded to any answer, no matter if it's the accepted one or not. – jgauffin Aug 03 '13 at 19:13
  • i have re-asked this exact question over here: "[Unload event for the default Application Domain?](http://stackoverflow.com/questions/18033100/unload-event-for-the-default-application-domain)". Hopefully someone can answer the question being asked there (which is identical to the question being asked here). – Ian Boyd Aug 15 '13 at 20:28
0

http://msdn.microsoft.com/en-us/library/system.appdomain.domainunload%28v=VS.90%29.aspx ..but again this is called before the domain is unloaded.

Taken from http://msdn.microsoft.com/en-us/library/system.appdomain.unload.aspx In the .NET Framework version 2.0 there is a thread dedicated to unloading application domains. This improves reliability, especially when the .NET Framework is hosted. When a thread calls Unload, the target domain is marked for unloading. The dedicated thread attempts to unload the domain, and all threads in the domain are aborted. If a thread does not abort, for example because it is executing unmanaged code, or because it is executing a finally block, then after a period of time a CannotUnloadAppDomainException is thrown in the thread that originally called Unload. If the thread that could not be aborted eventually ends, the target domain is not unloaded. Thus, in the .NET Framework version 2.0 domain is not guaranteed to unload, because it might not be possible to terminate executing threads.

Mahesh
  • 1,583
  • 13
  • 18
  • It does not run for the default AppDomain. –  Jan 03 '11 at 12:00
  • That is the point, the only piece that knows the default domain is being unloaded is the one who loaded it..and I don't think the default domain is loaded by managed code... – Mahesh Jan 03 '11 at 12:15
  • So you mean to say that unless I kick up a second AppDomain, I'm not going to be able to know when the default one unloads? –  Jan 03 '11 at 14:47
  • That is what I "Think", app domains are at the heart of the vm so you will need to dig into the internals..may be read a good book on vm internals.. – Mahesh Jan 04 '11 at 06:33
0

What about using a wrapper/launcher around your main application? That AppDomain could notice when its child ends (via blocking, polling or callback) and could perform whatever additional operations are needed.

Likewise, if main() consists of starting a single thread which does all the important stuff, then main() could also perform whatever additional work is needed.

Perhaps it would help if you could explain what you need to do in response to each AppDomain termination? It could be that what you are asking to do just isn't appropriate at the AppDomain level.

Bruce
  • 187
  • 2
  • 9
  • I need to deterministically dispose a series of objects that are managed by background threads that I start. Perhaps AppDomainManager could be useful here? –  Jan 05 '11 at 07:41
0

If your goal here is to cleanly dispose of objects that are used by some background threads, then my suggestion would be:

  1. When you want your want to exit the application, signal to the background threads that they should clean up and stop.

  2. From the main thread, use Thread.Join to wait for all the background threads to finish and exit.

Ran
  • 5,989
  • 1
  • 24
  • 26