Unity 中的 PerThreadLifetimeManager
在 Unity PerThreadLifetimeManager 文档中,我读到:“这个生命周期管理器不会处置它所持有的实例”。参考:http://msdn.microsoft.com/en-us/library /ff647854.aspx
因此,如果我使用 ThreadPool,是否意味着在 ThreadPool 的线程上使用 Unity 解析的对象在返回到该线程之前不会在该线程中完成的工作结束时被释放。水池?
我如何确保这些对象确实得到处置和处理的任何模式或想法我从线程池中得到一个干净的线程?
谢谢!
In the Unity PerThreadLifetimeManager documentation, I read that: "This lifetime manager does not dispose the instances it holds". Ref.: http://msdn.microsoft.com/en-us/library/ff647854.aspx
So, if I am using a ThreadPool, does it mean that objects resolved using Unity on a Thread of the ThreadPool will not get disposed at the end of the work done in that thread before being returned to the pool?
Any pattern or ideas how I can ensure that the objects do get disposed & I get a clean thread from the thread pool?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这取决于生命周期经理的类型。 PerThreadLifetimeManager 为每个线程维护一个实例。提供了六种类型的终身管理者,但这并不意味着如果它们不适合您就不能创建自己的终身管理者。
似乎您想要 TransientLifetimeManager 它提供每次调用一个新实例。您可以在工作线程中调用 Resolve,使用该实例,并在方法退出之前释放它。
我不确定您是否可以创建一个生命周期管理器,它会以某种方式自动知道您的线程已完成执行。确保发生这种情况的最佳方法是创建一个方法包装器,1) 从 Unity 获取实例(使用 TransientLifetimeManager),执行 Action。 (您的实际工作方法)传入实例,然后在退出之前处理该实例。然后,您可以通过将代码传递给在工作线程上运行的包装器来执行代码。您只需编写一次 Resolve() 和 Dispose() 代码,然后在任何地方重复使用它。
It depends on what type of lifetime manager. The PerThreadLifetimeManager maintains a single instance per thread. There are six provided types of lifetime managers, but that doesn't mean you can't make your own if they don't suit you.
Seems like you want the TransientLifetimeManager which provides a new instance per call. You can call Resolve within your worker thread, use the instance, and dispose of it before the method exits.
I'm not sure you could create a lifetime manager that would, somehow automatically, know that your thread has completed execution. The best way to ensure that would happen is to create a method wrapper that 1) gets the instance from Unity (using the TransientLifetimeManager), executes an Action<T> (your actual worker method) passing in the instance, and then disposes of that instance before exiting. Then you would execute your code by passing it to this wrapper, which is run on a worker thread. You'd only have to write your Resolve() and Dispose() code once and reuse it everywhere.