仅在 Visual Studio 2010 中调试期间,简单的 rx 代码在 Windows 窗体中静默失败 [英] simple rx code silently fails in windows forms only during debugging in visual studio 2010

查看:43
本文介绍了仅在 Visual Studio 2010 中调试期间,简单的 rx 代码在 Windows 窗体中静默失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感觉像bug和问题最近吸引了我!=P

所以我今天终于抽出时间来探索一个小小的 Rx.

这是我所做的:

这是唯一一段运行代码:

 private void button1_Click(object sender, EventArgs e){var txtc = Observable.FromEvent(textBox1, "TextChanged").Throttle(TimeSpan.FromSeconds(0.5)).SubscribeOnDispatcher();//**也试过.SubscribeOn(this)var t = from x in txtc select textBox1.Text;t.Subscribe(x => listBox1.Items.Add(x));}

现在,当运行调试 (F5) 时,我单击按钮,一切正常,然后我输入一些东西,噗!形式只是默默地死去!!

如果我在没有调试的情况下运行,应用程序将完美运行!

注意:我从 Form.Load 事件中删除了代码,因为在 Win7x64(是的,那是我的机器)上,VS 没有破坏该事件中的异常的已知错误

这是调试输出的样子:

<块引用><块引用>

线程 'vshost.NotifyLoad' (0x1438) 已退出,代码为 0 (0x0).

线程 'vshost.LoadReference' (0x155c) 已退出,代码为 0 (0x0).

'RxWinForms.vshost.exe' (Managed (v4.0.30319)): Loaded '\RxWinForms\bin\Debug\RxWinForms.exe', Symbols loaded.

System.InvalidOperationException"类型的第一次机会异常发生在 System.Windows.Forms.dll 中

程序[5228] RxWinForms.vshost.exe: Managed (v4.0.30319)"已退出,代码为 0 (0x0).

程序[5228] RxWinForms.vshost.exe:程序跟踪"已退出,代码为 0 (0x0).

解决方案

您需要确保在当前调度程序上发生了限制,或者在您之前通过 ObserveOn(而不是 SubscribeOn)切换回当前调度程序尝试更改 UI(我相信默认情况下节流是在 TaskPool 上完成的).

因此以下两种解决方案都有效:

private void button1_Click(object sender, EventArgs e){txtc = Observable.FromEvent(textBox1, "TextChanged").Throttle(TimeSpan.FromSeconds(0.5)).ObserveOn(Scheduler.Dispatcher);var t = 来自 txtc 中的 x选择 textBox1.Text;t.Subscribe(x => listBox1.Items.Add(x));}

private void button1_Click(object sender, EventArgs e){txtc = Observable.FromEvent(textBox1, "TextChanged").Throttle(TimeSpan.FromSeconds(0.5), Scheduler.Dispatcher)var t = 来自 txtc 中的 x选择 textBox1.Text;t.Subscribe(x => listBox1.Items.Add(x));}

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).

解决方案

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:

private void button1_Click(object sender, EventArgs e)
{
    txtc = Observable.FromEvent<EventArgs>(textBox1, "TextChanged")
       .Throttle(TimeSpan.FromSeconds(0.5))
       .ObserveOn(Scheduler.Dispatcher);

    var t = from x in txtc 
            select textBox1.Text;

    t.Subscribe(x => listBox1.Items.Add(x));
}

and

private void button1_Click(object sender, EventArgs e)
{
   txtc = Observable.FromEvent<EventArgs>(textBox1, "TextChanged")
      .Throttle(TimeSpan.FromSeconds(0.5), Scheduler.Dispatcher)

   var t = from x in txtc 
           select textBox1.Text;

   t.Subscribe(x => listBox1.Items.Add(x));
}

这篇关于仅在 Visual Studio 2010 中调试期间,简单的 rx 代码在 Windows 窗体中静默失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆