ObserveOnDispatcher不会调用UI线程中的处理程序

本文关键字:处理 程序 线程 UI 调用 ObserveOnDispatcher | 更新日期: 2023-09-27 18:12:50

我有一个消息总线类,它使用Rx在WPF应用程序中推送多个线程事件。我的问题是ObserveOnDispatcher没有在UI线程中调用事件处理程序。

代码:

private void button_Click(object sender, RoutedEventArgs e)
{
  var messageBus = new MessageBus();
  messageBus.GetMessages<Message>().ObserveOnDispatcher().Subscribe(x => TestHanlder(x));
  Trace.WriteLine("Main Thread Id:" + Thread.CurrentThread.ManagedThreadId);
  var deviceManager = new DeviceManager(messageBus);
  deviceManager.Start();
}
private void TestHanlder(Message message)
{
  Trace.WriteLine("UI Handler ThreadId:" + Thread.CurrentThread.ManagedThreadId);
}
public class DeviceManager
{
  private readonly MessageBus _messageBus;
  public DeviceManager(MessageBus messageBus)
  {
    _messageBus = messageBus;
  }
  public void Start()
  {
    for (;;)
    {
      var t = Task.Factory.StartNew(() => BackGroundTask(), TaskCreationOptions.LongRunning);
      t.Wait();
    }
  }
  private void BackGroundTask()
  {
    Thread.Sleep(1000);
    Trace.WriteLine("Push ThreadId:" + Thread.CurrentThread.ManagedThreadId);
    var message = new Message();
    _messageBus.Publish(message);
  }
}
public class MessageBus
{
  readonly ISubject<object> _messages;
  public MessageBus()
  {
    _messages = new Subject<object>();
  }
  public void Publish<TMessage>(TMessage message)
  {
    _messages.OnNext(message);
  }
  public IObservable<TMessage> GetMessages<TMessage>()
  {
    return _messages.OfType<TMessage>();
  }
}
public class Message
{
  public Message()
  {
  }
}

Without ObserveOnDispatcher:
messageBus.GetMessages<Message>().Subscribe(x => TestHanlder(x));
..........................Output.................................
Main Thread Id:8
Push ThreadId:9
UI Handler ThreadId:9
But I need to execute the TestHanlder function in the main thread or UI thread, in my use case above it must be the thread number 8.
When I use ObserveOnDispatcher:
messageBus.GetMessages<Message>().ObserveOnDispatcher().Subscribe(x => TestHanlder(x));
..........................Output.................................
Main Thread Id:9
Push ThreadId:10
 ------------------> UI Handler ThreadId: are missing not there!?
What I'm doing wrong here?!!!!!

ObserveOnDispatcher不会调用UI线程中的处理程序

for (;;) { t.Wait(); }此代码在UI线程中执行,并阻止它执行在其上调度的任何其他内容。ObserveOnDispatcher工作正常,但是你的dispatcher线程被阻塞了。

如果你引入async/await(这将释放线程),场景将工作得很好:

    private async void button_Click(object sender, RoutedEventArgs e)
    {
        var messageBus = new MessageBus();
        messageBus.GetMessages<Message>().ObserveOnDispatcher().Subscribe(x => TestHanlder(x));
        Trace.WriteLine("Main Thread Id:" + Thread.CurrentThread.ManagedThreadId);
        var deviceManager = new DeviceManager(messageBus);
        await deviceManager.Start();
    }
    ...
        public async Task<Unit> Start()
        {
            for (;;)
            {
                await Task.Factory.StartNew(() => BackGroundTask(), TaskCreationOptions.LongRunning);
            }
        }