gpt4 book ai didi

c# - ObserveOnDispatcher 不调用 UI 线程中的处理程序

转载 作者:行者123 更新时间:2023-11-30 16:06:13 35 4
gpt4 key购买 nike

我有一个消息总线类,它使用 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?!!!!!

最佳答案

for (;;) { t.Wait(); } 此代码在 UI 线程中执行,并阻止它执行任何其他分配给它的东西。 ObserveOnDispatcher 工作正常,但您的调度程序线程被阻塞。

如果您引入 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);
}
}

关于c# - ObserveOnDispatcher 不调用 UI 线程中的处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32565665/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com