I pass a data access class (DAL) of my own making into a another class so it can use those data access methods to store data in my sqlite database.
This is type-safe because each method that accesses the database creates it’s own connection object so calls to the connection object always come from the same thread it was created on.
I’d like to just use one connection (or a pool of connection objects) in my DAL class and this would work fine if all calls to my DAL came from the main UI thread.
Is there a way for my DAL class to use a connection object (or pool of them) when called from the main thread but then use a different connection object if is being called from a background thread?
You can call
InvokeRequiredon any WinFormsControl-derived type to determine if you’re on the main thread or not. IfInvokeRequiredreturnstrue, then you are not on the main UI thread.If you’re using WPF, you can call
Dispatcher.CheckAccesson theDependencyObject.Dispatcherproperty of one of your UI elements. IfCheckAccessreturns true, it’s the main UI thread.