PerThreadLifetimeManager in Unity

痞子三分冷 提交于 2019-12-06 06:27:18

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!