我应该如何从 P&P 为 EventAggregator 创建事件,以便 UI 线程上的订阅者可以收听它们?

发布于 2024-08-12 05:16:34 字数 1038 浏览 3 评论 0原文

我正在尝试在后台任务运行时更新主窗体中的进度条。

我正在使用最新的 Patterns & 中的 EventAggregator。实践发布路线我的应用程序范围内的事件。

我正在从一个监听BackgroundWorker事件的类中触发一个事件,然后触发一个事件,如下所示:

  1. Process on bw fires the BW method 报告进展情况。
  2. BW 火了 报告事件。
  3. 他们被选中 通过 SomeCommand 类方法 之前已在 BW 上设置 推出。
  4. 我发布事件来自 EventAggregator

public void ProgressChanged(对象发送者,ProgressChangedEventArgs ea) { KnownProgressStatusChangedEvent evt = KernelKeeper.Kernel.Get().GetEvent(); evt.Publish(ea); 我

的 MainPresenter 已订阅这些事件,如下所示:

    KnownProgressStatusChangedEvent progressChanged = EventAggregator.GetEvent<KnownProgressStatusChangedEvent>();
    progressChanged.Subscribe(KnownProgressChanged,ThreadOption.UIThread);

如果我不设置 ThreadOption.UIThread,我会在 Program.cs 中收到 TargetInvokationException,且没有堆栈跟踪。 这样我就不会出现异常,并且可以进入 EventAggregator。

当它要调用 KnownProgressChanged 方法时,它会尝试调用该方法并检查 Application.Current != null。它为空并且没有任何内容被触发。

我做错了什么?请指教。

I am trying to update a progress bar in my main form while a background task is running.

I am using the EventAggregator from the latest Patterns & Practices release route my application wide events.

I am firing an event from a class listens to BackgroundWorker events and than fires an event as such:

  1. Process on bw fires the BW method
    to report progress.
  2. BW fires it's
    reporting events.
  3. They get picked
    up by the SomeCommand class methods
    were set on the BW before it was
    launched.
  4. I publish Events from
    the EventAggregator

public void ProgressChanged (object sender, ProgressChangedEventArgs ea)
{
KnownProgressStatusChangedEvent evt = KernelKeeper.Kernel.Get().GetEvent();
evt.Publish(ea);
}

My MainPresenter has subscribed to those events as such:

    KnownProgressStatusChangedEvent progressChanged = EventAggregator.GetEvent<KnownProgressStatusChangedEvent>();
    progressChanged.Subscribe(KnownProgressChanged,ThreadOption.UIThread);

If I don't set the ThreadOption.UIThread I get TargetInvokationException in the Program.cs with no stack trace.
This way I get no exception and I can step in the EventAggregator.

When it is about to call the KnownProgressChanged method it tries to invoke it and checks for Application.Current != null. It is null and nothing is fired.

What am I doing wrong ? Please advise.

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

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

发布评论

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

评论(2

凉宸 2024-08-19 05:16:34

您必须指定 ThreadOption.UIThread 因为您正在处理进度条,必须从 ui 线程调用处理程序才能绘制新的进度状态。

但是,如果您使用 WPF,则必须在没有 ThreadOption.UIThread 和 dispatch 自己调用,您可以查看 CompositeWpfEvent。

请参阅事件聚合器 - 在用户界面线程上订阅

订阅者通常需要
更新用户界面元素
对事件的响应。在 Windows 中
演示基础 (WPF),仅
UI线程可以更新用户界面
元素。默认情况下,订阅者
接收发布者上的事件
线程所以如果发布者发送
来自 UI 线程的事件
订户将能够更新
用户界面。

但是,如果发布者的线程是
后台线程,订阅者
可能无法直接更新用户
界面元素。相反,它会
需要安排 UI 上的更新
使用 Windows 演示文稿的线程
基金会的 Dispatcher 类。这
CompositeWpfEvent 提供了
复合应用程序库可以
通过允许订户提供协助
自动接收事件
用户界面线程。订户必须
在订阅期间指出这一点,如
如下面的代码所示。

...

You have to specify ThreadOption.UIThread because you are dealing with a progress bar, the handler must be called from the ui thread to be able to draw the new progress state.

Howerver if you are working with WPF you have to handle it without ThreadOption.UIThread and dispatch the call yourself, you may take a look at the CompositeWpfEvent.

See Event Aggregator - Subscribing on the User Interface Thread

Frequently, subscribers will need to
update user interface elements in
response to events. In Windows
Presentation Foundation (WPF), only a
UI thread can update user interface
elements. By default, the subscriber
receives the event on the publisher's
thread so if the publisher sends the
event from the UI thread, the
subscriber will be able to update the
user interface.

However, if the publisher's thread is
a background thread, the subscriber
may be unable to directly update user
interface elements. Instead, it would
need to schedule the updates on the UI
thread using the Windows Presentation
Foundation's Dispatcher class. The
CompositeWpfEvent provided with the
Composite Application Library can
assist by allowing the subscriber to
automatically receive the event on the
UI thread. The subscriber must
indicate this during subscription, as
shown in the following code.

...

情栀口红 2024-08-19 05:16:34

TargetIncationException 是一个转移注意力的问题,它是我在代码中的其他地方抛出未实现的异常而引起的。

我正在使用 WinForms 并使用带有 ThreadOption.PublisherThread 选项的订阅,效果很好。

TargetInvocationException was a red herring, which I caused by throwing a not implemented exception elsewhere in my code.

I am using WinForms and used the Subscribe with the ThreadOption.PublisherThread option and that works fine.

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