仅在 Visual Studio 2010 中调试期间,简单的 Rx 代码在 Windows 窗体中默默失败
感觉最近 bug 和问题吸引了我! =P
所以我今天终于花了一些时间来探索一些 Rx。
这是我所做的:
这是唯一一段正在运行的代码:
private void button1_Click(object sender, EventArgs e)
{
var txtc = Observable.FromEvent<EventArgs>(textBox1, "TextChanged")
.Throttle(TimeSpan.FromSeconds(0.5))
.SubscribeOnDispatcher();//**also tried .SubscribeOn(this)
var t = from x in txtc select textBox1.Text;
t.Subscribe(x => listBox1.Items.Add(x));
}
现在,当运行调试 (F5) 时,我单击按钮,一切都好,然后我输入一些东西,噗!形体就这样悄无声息地死去!!
如果我在没有调试的情况下运行,应用程序将完美运行!
注意:我从 Form.Load 事件中删除了代码,因为 VS 在 Win7x64< 上的该事件中不会中断异常的已知错误/strong> (是的,这就是我的机器)
调试输出如下所示:
<块引用>线程“vshost.NotifyLoad”(0x1438) 已退出,代码为 0 (0x0)。
线程“vshost.LoadReference”(0x155c) 已退出,代码为 0 (0x0)。
“RxWinForms.vshost.exe”(托管 (v4.0.30319)):已加载“\RxWinForms\bin\Debug\RxWinForms.exe”,已加载符号。
System.Windows.Forms.dll 中第一次出现“System.InvalidOperationException”类型的异常
程序“[5228] RxWinForms.vshost.exe:托管 (v4.0.30319)”已退出,代码为 0 (0x0)。
程序“[5228] RxWinForms.vshost.exe:Program Trace”已退出,代码为 0 (0x0)。
Feels like bugs and problems are attracted to me lately! =P
So I finally took some time off today to explore a little Rx.
Heres what I did:
Here's the only piece of running code:
private void button1_Click(object sender, EventArgs e)
{
var txtc = Observable.FromEvent<EventArgs>(textBox1, "TextChanged")
.Throttle(TimeSpan.FromSeconds(0.5))
.SubscribeOnDispatcher();//**also tried .SubscribeOn(this)
var t = from x in txtc select textBox1.Text;
t.Subscribe(x => listBox1.Items.Add(x));
}
Now, when run Debug (F5) I click the Button, all good, I then type something, poof! The form just silently dies!!
If I run without debugging, the application runs flawlessly!
Note: I removed the code from the Form.Load event because of the known bug with VS not breaking on exceptions in that event on Win7x64 (and yes thats my machine)
This is what the debug output looks like:
The thread 'vshost.NotifyLoad' (0x1438) has exited with code 0 (0x0).
The thread 'vshost.LoadReference' (0x155c) has exited with code 0 (0x0).
'RxWinForms.vshost.exe' (Managed (v4.0.30319)): Loaded '\RxWinForms\bin\Debug\RxWinForms.exe', Symbols loaded.
A first chance exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll
The program '[5228] RxWinForms.vshost.exe: Managed (v4.0.30319)' has exited with code 0 (0x0).
The program '[5228] RxWinForms.vshost.exe: Program Trace' has exited with code 0 (0x0).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在尝试更改 UI 之前,您需要确保当前调度程序上发生了限制,或者通过 ObserveOn(而不是 SubscribeOn)切换回当前调度程序(我相信默认情况下,限制是在 TaskPool 上完成的) )。
所以下面的两个解决方案都有效:
和
You need to make sure that either the Throttling is happening on the current dispatcher or that you switch back on to the current dispatcher through ObserveOn (not SubscribeOn) before you try and change the UI (I believe that by default Throttling is done on the TaskPool).
So both of the solutions below work:
and
詹姆斯是对的。但是,建议您使用方法的
IScheduler
重载(例如Throttle
),而不是使用默认重载,然后使用ObserveOn
(这将导致它跳转到任务池,然后返回调度程序)。James is correct. However, it is recommended that you use the
IScheduler
overload of methods (likeThrottle
, rather than using the default overload and then usingObserveOn
(which will cause it to jump to the task pool and then back to the dispatcher).