Project is C#.
So I have a bunch of multithreaded code that is designed to be run as a library. It’s in a seperate project from the UI.
My library has a central object that needs to be created before anything that would fire off events is created.
Is it possible for this master object to be passed in some objects so that my events can figure out when they would need to be invoked to get back to the main UI thread?
I’d really like to keep the UI from have to do a bunch of invoking since his event handlers are almost always going to be called from some random background thread.
From what you’re describing, your best option might be to make all of your objects take a SynchronizationContext as a argument in their constructor.
If you do that, when you need to raise an event, you can use SyncrhonizationContext.Post to “invoke” the event on the UI thread.
This way, your library will be UI-agnostic (could work in Windows Forms or WPF), and you can raise events on the UI thread as needed.
The user of your library would just create your object using SynchronizationContext.Current (which gives you a valid context in Windows Forms and WPF applications, without pulling in a reference to either).