ReactiveUI:无法让代码在后台线程上运行 [英] ReactiveUI: Can't get code to run on background thread
问题描述
只是涉足 RxUI 并试图让一个 noddy 示例工作......
我有一个带有列表框和按钮的 WPF 视图.当我按下按钮 (Go) 时,我想在后台线程上运行一个方法,并将它产生的结果添加到 ListBox.我正在记录线程 id 以检查正在执行的内容.问题是我总是看到所有操作都发生在同一个线程上.我尝试在 CreateAsyncObservable 上指定 Scheduler.Default,但随后没有向 ListBox 添加任何内容.
公共类 MainViewModel : ReactiveObject{公共主视图模型(){结果 = new ReactiveList();var seq = ReactiveCommand.CreateAsyncObservable(_ => GetAsyncResults());seq.ObserveOn(Scheduler.CurrentThread);seq.Subscribe(s =>{Results.Add(string.Format("{0} thread {1}", s, Thread.CurrentThread.ManagedThreadId));});Results.Add(string.Format("主线程{0}", Thread.CurrentThread.ManagedThreadId));Go = ReactiveCommand.Create();Go.Subscribe(_ => seq.Execute(null));}公共静态 IObservable获取异步结果(){线程睡眠(1000);return (new[] {"Rod", "Jane", "Freddy"}).ToObservable();}private readonly ObservableAsPropertyHelper>_字符串;公共列表<字符串>字符串 {get { 返回 _strings.Value;}}public ReactiveCommand
Rx 在您提出要求之前不会切换线程,并且 GetAsyncResults 只是同步返回项目列表.您需要指定 RxApp.TaskpoolScheduler
将内容移动到后台线程.
public static IObservable获取异步结果(){return (new[] {"Rod", "Jane", "Freddy"}).ToObservable(RxApp.TaskpoolScheduler);}
<块引用>
seq.ObserveOn(Scheduler.CurrentThread);
你没有使用 this 的返回值,所以它什么都不做
<块引用>Go = ReactiveCommand.Create();
为什么要在这里创建两个单独的命令?
Just dabbling with RxUI and trying to get a noddy example to work...
I've got a WPF view with a ListBox and a button. When I press the button (Go) I want to run a method on a background thread and have the results it produces be added to the ListBox. I'm logging the thread id to check what's executing where. The problem is I always see all the operations happening on the same thread. I've tried specifying Scheduler.Default on the CreateAsyncObservable but then nothing gets added to the ListBox.
public class MainViewModel : ReactiveObject
{
public MainViewModel()
{
Results = new ReactiveList<string>();
var seq = ReactiveCommand.CreateAsyncObservable(_ => GetAsyncResults());
seq.ObserveOn(Scheduler.CurrentThread);
seq.Subscribe(s =>
{
Results.Add(string.Format("{0} thread {1}", s, Thread.CurrentThread.ManagedThreadId));
});
Results.Add(string.Format("main thread {0}", Thread.CurrentThread.ManagedThreadId));
Go = ReactiveCommand.Create();
Go.Subscribe(_ => seq.Execute(null));
}
public static IObservable<string> GetAsyncResults()
{
Thread.Sleep(1000);
return (new[] {"Rod", "Jane", "Freddy"}).ToObservable();
}
private readonly ObservableAsPropertyHelper<List<string>> _strings;
public List<string> Strings {get { return _strings.Value; }}
public ReactiveCommand<object> Go { get; protected set; }
public ReactiveList<string> Results { get; set; }
}
Rx doesn't switch threads until you ask it, and GetAsyncResults simply returns a list of items synchronously. You need to specify RxApp.TaskpoolScheduler
to move stuff to a background thread.
public static IObservable<string> GetAsyncResults()
{
return (new[] {"Rod", "Jane", "Freddy"}).ToObservable(RxApp.TaskpoolScheduler);
}
seq.ObserveOn(Scheduler.CurrentThread);
You didn't use the return value of this, so it doesn't do anything
Go = ReactiveCommand.Create();
Why are you creating two separate commands here?
这篇关于ReactiveUI:无法让代码在后台线程上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!