What's the best approach/pattern I should use for the following?
- Have a C# UI solution that will have (a) Winforms/WPF UI, and (b) class library.
- The UI will have to start a separate thread for the routine in the class library that will be polling
- The class library will then need to trigger a callback function in the UI to update UI fields if necessary
How do I best implement this callback arrangement so that the class library can be reused, and ideally it should have no depende开发者_高级运维ncy on the UI component.
thanks
Use Backgroundworker for this. This will ease your implementation and thread management.
You could do this via an Event being raised out of your library, this way the library will only need to pop the event, if the UI chooses to consume the event then that's implementation in the UI.
You could also expose a property eg CallbackMethod and have a delegate that it accepts, then in the UI you set the property to the addressof your function. When the library needs to "send the necessary callback" you check to see if the callback has been populated, then call the callback function (via the delegate) if it's been set, or just skip it if it hasn't
精彩评论