.NET 中的事件和多线程代码

发布于 2024-08-16 03:34:56 字数 211 浏览 9 评论 0原文

项目是C#。

所以我有一堆多线程代码,旨在作为库运行。它位于与 UI 不同的项目中。

我的库有一个中心对象,需要在创建任何会触发事件的对象之前创建该对象。

是否可以将此主对象传递到某些对象中,以便我的事件可以确定何时需要调用它们以返回主 UI 线程?

我真的很想让 UI 不必进行大量调用,因为他的事件处理程序几乎总是从一些随机后台线程中调用。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

阳光下的泡沫是彩色的 2024-08-23 03:34:56

根据您的描述,最好的选择可能是让所有对象采用 SynchronizationContext 作为其构造函数中的参数。

如果这样做,当您需要引发事件时,可以使用 SyncrhonizationContext.Post 来“调用”UI 线程上的事件。

这样,您的库将与 UI 无关(可以在 Windows 窗体或 WPF 中工作),并且您可以根据需要在 UI 线程上引发事件。

您的库的用户只需使用 SynchronizationContext 创建您的对象。当前(它为您提供 Windows 窗体和 WPF 应用程序中的有效上下文,而不提取对两者的引用)。

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).

小草泠泠 2024-08-23 03:34:56

使用计时器或响应事件,UI 线程(或线程上的数据绑定对象)可以轮询“状态”对象并限制 UI 必须执行的工作量。

Using a timer, or responding to an event, the UI thread (or a databound object on the thread) could poll a "status" object and limit the amount of work the UI must do.

饮惑 2024-08-23 03:34:56

您是否看过外观代理工厂单例 模式?

如果您将核心对象实现为单例,并提供代理和工厂方法来生成这些对象并将其传递给库的调用者,这将变得相当简单。外观模式可以通过抽象出后端发生的大部分内部情况来使这变得更容易。

结合 Factory 和 Singleton,并确保您想要公开的所有类都具有需要主对象的构造函数,这是强制执行此设计的好方法。我不知道您的图书馆的主要要求/设计,所以我不确定这是否适合您。

Have you looked at the Facade, Proxy, Factory and Singleton patterns?

If you implement your core object as a Singleton, and provide proxy and factory methods to produce and deliver these to callers of your library this becomes fairly straight-forward. The Facade pattern can make this easier by abstracting away most of the internals of what is going on in the backend.

Combining Factory and Singleton, and ensuring that all classes you want exposed have constructors requiring your main object is a good way to enforce this design. I dont know the main requirements/design of your library, so I am not sure if this would work for you or not.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文