Closed sa-es-ir closed 4 months ago
you want to make sure this isn't done in multiple places, if you have multiple JoinableTaskFactories/Contexts they don't interop, the framework is supposed to be shared by everyone last time I checked (deadlock prevention won't work properly across different contexts since they aren't aware of each other)
so yes, its bad practice to locally create a JTF/JTC if you can't guarantee its the only place, better practice would be registering them as a (singleton) service to share them across all DI services
Thanks @weltkante for the response, by this registering them as a (singleton) service to share them across all DI services
do you mean registering JoinableTaskContext
and JoinableTaskFactory
as a singleton, and every time we get them from DI and use them without instantiating a new one?
Registering one of them is sufficient since you can get the default factory from the context (actually don't have to create one) or get the context from the factory. You can have multiple factories for a context, but if you don't need that you can register the factory directly as a singleton in the DI. I don't know any particular "best practices" over which of them should be retrievable via DI, I guess playing it safe would be to register both, if you don't know what the DI services will import.
@weltkante is spot on. I'll just add that even if the quoted code is the "only place" that you create a new JoinableTaskContext()
, it may still be faulty if that code executes more than once in you application, for the reasons @weltkante gave.
I'm going to close this as it isn't an issue with the library. Feel free to carry the discussion on, preferably in the Discussions tab.
Today I saw a piece of code like this:
For sure it is working but I was curious whether it's a good practice to use or not? Is there any caveats here?
Package:
Microsoft.VisualStudio.Threading
Version:17.9.28