How do I execute a block of code with PostSharp when the application exits?
Presently, I am checking th开发者_JS百科e method name in the OnMethodBoundaryAspect.OnExit method:
[Serializable]
public class TimerAttribute : OnMethodBoundaryAspect
{
public override void OnExit(MethodExecutionEventArgs eventArgs)
{
if(eventArgs.Method.DeclaringType.Name == "Program" && eventArgs.Method.Name == "Main")
//do things
}
}
Obviously, this is ugly and feels kludgy. Is there a more robust way to detect application exit with PostSharp?
I don't think you should use PostSharp to do that.
You can detect whether the application domain is exiting by using some features of System.AppDomain:
- AppDomain.IsFinalizingForUnload()
- AppDomain.ProcessExit
- AppDomain.DomainUnload
精彩评论