simple rx code silently fails in windows forms only during debugging in visual studio 2010

蹲街弑〆低调 提交于 2019-12-11 01:17:17

问题


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


回答1:


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));
}



回答2:


James is correct. However, it is recommended that you use the IScheduler overload of methods (like Throttle, rather than using the default overload and then using ObserveOn (which will cause it to jump to the task pool and then back to the dispatcher).



来源:https://stackoverflow.com/questions/4655619/simple-rx-code-silently-fails-in-windows-forms-only-during-debugging-in-visual-s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!