I am designing a WPF application following MVVM pattern. But when it comes to multi-threading issue, I find my knowledge quite limited. My question is exactly like the title: can I create the ViewModel
objects in a worker thread?
Currently we are not using any dependency injection so I just create the ViewModel object in the constructor of the View
in UI thread (I guess it is not the best practice). But now there is a problem: we have one core class, let's call it CoreContext
, from our legacy software which is responsible for many core tasks, Data Access Layer between our SW and database is one of them. Many of our ViewModel
hold one reference to CoreContext
as data member. Unfortunately this class is not built thread-safe. When I create the ViewModel
instance(and hence the CoreContext
instance) in UI thread, it is guaranteed that CoreContext
is only accessed in the owning thread. However, when it involves with some heavy database query, the UI becomes not responsive.
So now I am wondering, is it possible to instantiate the ViewModel
(hence the CoreContext
instance) in a non-UI thread so the UI can be responsive when the VM(ultimately the CoreContext
) is making heavy query?