开发者

what pattern should I use to couple .NET UI to a .NET library class that will provide callback updates

What's the best approach/pattern I should use for the following?

  1. Have a C# UI solution that will have (a) Winforms/WPF UI, and (b) class library.
  2. The UI will have to start a separate thread for the routine in the class library that will be polling
  3. 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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜