Unity 中的 PerThreadLifetimeManager [英] PerThreadLifetimeManager in Unity

查看:20
本文介绍了Unity 中的 PerThreadLifetimeManager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Unity PerThreadLifetimeManager 文档中,我读到:这个生命周期管理器不会处理它持有的实例".参考:http://msdn.microsoft.com/en-us/library/ff647854.aspx

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

那么,如果我使用 ThreadPool,是否意味着在 ThreadPool 的线程上使用 Unity 解析的对象在返回到池之前不会在该线程中完成的工作结束时被处理?

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?

谢谢!

推荐答案

这取决于生命周期管理器的类型.PerThreadLifetimeManager 为每个线程维护一个实例.提供了六种类型的终身经理,但这并不意味着如果它们不适合您,您就不能创建自己的经理.

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.

似乎您想要 TransientLifetimeManager 每次调用提供一个新实例.您可以在工作线程中调用 Resolve,使用该实例,并在方法退出之前处理它.

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.

我不确定您是否可以创建一个生命周期管理器,它会以某种方式自动知道您的线程已完成执行.确保发生这种情况的最佳方法是创建一个方法包装器,它 1) 从 Unity 获取实例(使用 TransientLifetimeManager),执行传入该实例的 Action(您的实际工作方法),然后处理该实例退出前的实例.然后,您将通过将代码传递给这个在工作线程上运行的包装器来执行您的代码.您只需编写一次 Resolve() 和 Dispose() 代码,即可在任何地方重复使用.

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.

这篇关于Unity 中的 PerThreadLifetimeManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆