Simple rx code silently crashes in windows forms only during debugging in visual studio 2010

It seems like I’ve been attracted to errors and problems lately! = P

So, I finally took some time to research a bit of Rx.

Here is what I did:

alt text

Here is just a piece of 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 you start Debug (F5), I press the button, everything is fine, then I type something, poof! The form just dies quietly !!

If I run without debugging, the application works flawlessly!

Note. I removed the code from the Form.Load event due to a known error when VS did not break the exceptions in this event on Win7x64 (and yes, this is my machine)

Here is what the debug output looks like:

The stream 'vshost.NotifyLoad' (0x1438) exited with code 0 (0x0).

The stream 'vshost.LoadReference' (0x155c) exited with code 0 (0x0).

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

The first random error like "System.InvalidOperationException" occurred in System.Windows.Forms.dll

The program '[5228] RxWinForms.vshost.exe: Managed (v4.0.30319)' exited with code 0 (0x0).

The program "[5228] RxWinForms.vshost.exe: Program Trace" exited with code 0 (0x0).

+2
source share
2 answers

You need to make sure that either Throttling is happening on the current dispatcher, or that you are returning to the current dispatcher via ObserveOn (not SubscribeOn) before trying to change the user interface (I believe that by default Throttling runs on TaskPool).

So both of the solutions below are:

 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)); } 
+3
source
James is right. However, it is recommended to use the IScheduler method IScheduler (for example, Throttle , rather than using the default overload, and then use ObserveOn (which will cause it to go to the task pool and then back to the dispatcher).
+2
source

All Articles