ekonbenefits / impromptu-interface

Static interface to dynamic implementation (duck casting). Uses the DLR combined with Reflect.Emit.
Apache License 2.0
656 stars 67 forks source link

ImpromptuMVVM: Dispatcher thread awareness #5

Closed jbtule closed 7 years ago

jbtule commented 11 years ago

Reported by @WorldMaker Sep 21, 2012

Description of feature:

Silverlight, WPF, and Win8 all use a threading model where UI changes need to be marshalled (back) onto a "Dispatcher" thread (the UI thread). Good performant apps run operations on a background thread and then marshall changes back to the UI/Dispatcher thread.

C# 4.5 support for the async/await keywords help mitigate handling this dispatcher thread work in non-MVVM event handlers and it would nice for ImpromptuMVVM to support Dispatcher thread awareness as well.

The suggestion here would be to add the necessary checks to the pieces that call INotifyPropertyChanged, CommandExecuteChanged, etc events to check if they are running on the appropriate Dispatcher thread, and if they are not to call the suitable BeginInvoke(...) to do so.

A further suggestion to help make even easier to "do things the right way by default" would be to either default impromptu Commands to run on a background thread. It may be a breaking change to do that, so a better suggestion may be to support impromptu Commands that return Task (and by extension the C# 4.5 async keyword) and make sure those run in a background thread.

Usage case:

Contract.Prop1 = "value"; // ...more operation code here... Dynamic.Prop2 = "other value";

Instead of:

App.Current.Dispatcher.BeginInvoke(() => { Contract.Prop1 = "value"; }); // ...more operation code here... App.Current.Dispatcher.BeginInvoke(() => { Dynamic.Prop2 = "other value"; });

Also:

// Background commands public Task Save(object p) {} public async void Save(object p) {}

Notes:

I'm willing to contribute code to support this, but before I started coding something for this I wanted to make sure I reported it as a feature request to get a feel for whether or not there is deeper interest in this idea and if it feels like something that should indeed be a part of ImpromptuMVVM. Oct 26, 2012 Delete comment Project Member jtuley I think the dispatcher idea is a really good one. The task async stuff, is interesting, I think the backwards compatibility without much overhead could be done with a slight syntax change such as CommandAsync.Save or something to that effect. Would welcome code.

FYI on my github I have my more bleeding edge stuff now. including the solution updated for VS2012 https://github.com/jbtule/impromptu-interface Nov 3, 2012 Delete comment max.battcher I forked it on github last weekend and think I actually got this coded. I haven't sent a pull request yet because I think I want to build a simple quick WPF app to test it a bit, but the code is there if you want to see what I've attempted:

https://github.com/WorldMaker/impromptu-interface Jan 12, 2013 Delete comment max.battcher Quick update that in the last few weeks I've moving towards a different approach to similar ideas here: I've been working in small amounts of spare time to try my hand at hybridizing ImpromptuInterface.MVVM with ReactiveUI (reactiveui.net)... Jan 31, 2013 Delete comment max.battcher I've posted my port of ImpromptuInterface.MVVM on top of ReactiveUI thus far to github: https://github.com/WorldMaker/ReflexUX

There is still a bunch of things on my TODO list and it needs a sample application, but there should be enough code there to get an idea of what I'm trying to do with it. I'd appreciate any feedback you might have. Jan 31, 2013 Delete comment Project Member jtuley Wish I was more familiar with ReactiveUI, looks clean, very interested in seeing a sample app! Made some comments on git hub too. Jan 31, 2013 Delete comment max.battcher Yes, thanks a lot for the comments. Here's my first crack at a sample app by forking and branching an existing RxUI sample: https://github.com/WorldMaker/RxUI_QCon/tree/reflex

There's still plenty I want to do with this, but I think this shows a good start.

jbtule commented 7 years ago

MVVM is gone