开发者

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜