-
Notifications
You must be signed in to change notification settings - Fork 1.4k
[WIP] Dispatcher Helper #551
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Hi @Code-ScottLe, I'm your friendly neighborhood .NET Foundation Pull Request Bot (You can call me DNFBOT). Thanks for your contribution! TTYL, DNFBOT; |
@Code-ScottLe, Thanks for signing the contribution license agreement so quickly! Actual humans will now validate the agreement and then evaluate the PR. |
@deltakosh @lukasf @jlnostr Any feedback regarding the signature of the helpers would be great |
@Code-ScottLe If you ask me, the methods could be combined if "viewToExecuteOn" is last with null defaulting to MainWindow. Then only half the amount of methods would be needed. But more importantly, I would recommend to remove the null propagation. If the methods are called on a null dispatcher or view, then something must have gone wrong. It's better let the dev know by raising a NullReferenceException, rather than silently dropping the call. |
@lukasf Personally i prefer the overloaded method for handling the scenario of default view to be main view. It makes the code from the user stand point cleaner and easier to use than having to explain to them that they have to pass |
@Code-ScottLe Agreed, maybe this is the better way to do it explicitly here. As i said, I have more concerns about the null propagation dropping developer errors silently. |
@lukasf That is one of the thing that i noticed after the PR yesterday, that the call would fail silently in the case of null view. I think we should perform a check and throw |
Agree on the exception. We cannot swallow error. |
@deltakosh Look like we don't have any problem with the signature of the helpers. I will make example and document for it very soon. The null check will be done along with it |
I confirm: API looks good to me |
Just one question:
|
|
Ok so perhaps the name should be clearer (at least to me) |
Any others bright ideas for naming? I'm not too comfortable with that name TBH, but that is the best one that i can come up with. |
Should I bring the fact that I'm french? :) |
How about ScheduleAsync and ExecuteAsync ? |
sorry last in terms of capability but first in terms of time :) |
Good point on context switching by @dotMorten , i will fix it on the next commit |
… helper back to the correct one.
@dotMorten is this good to go yet? @deltakosh I am starting to do the documentation :) , I don't know if i can unit test this thing though. |
Alright, I have decided to add in support for synchronous code for the API as well (through
Before having support for synchronous code, they have to tag the lambda |
@Code-ScottLe Excellent, I did not realize that this was missing, it sure makes sense. |
So something like DispatcherHelper.ExecuteOnUIThreadAndReturnImmediatly? (with a good name) based on how WinRT is build I think we should not do it because we should do it for every async method then. beside that I like the helper as it is right now |
It does not return immediately though, as it will still propagate back a I ran into the same trouble with the Microsoft Band SDK before, as the extension method they built to handle the bitmap was synchronous and required to be run under UI Thread. Of course i can still make an async lambda and return null, but it was just too clunky and Visual Studio will yell at you for having an async function without an "await" keyword. Definitely open up for changes though |
Because we are close to code freeze, I would suggest to leave it like this and think about adding sync methods for 1.3. Does it make sense? |
I have already added it in, do you want me to remove it? :D It was done along side with the documentation and sample page (as this problem was coming from making the sample page) |
I must be missing something but I do not see sync methods? Should I? (I guess so based on your comment :D) |
Ah, we are not talking about adding a synchronous version of the Before,
So by adding an overload of |
Ok! gotcha |
Sorry @Code-ScottLe, I think a merge I've just done has now given this conflicts. |
@ScottIsAFool Look like we just merge in a change that has new sample page. No worries, I will fix it. |
@ScottIsAFool Try again now. |
/// <param name="function">Synchronous function with return type <typeparamref name="T"/> to be executed on UI thread</param> | ||
/// <param name="priority">Dispatcher execution priority, default is normal</param> | ||
/// <returns>Awaitable Task with type <typeparamref name="T"/></returns> | ||
public static Task<T> ExecuteOnUIThreadAsync<T>(CoreApplicationView viewToExecuteOn, Func<T> function, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Quick question, why isn't this an extension method?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It takes away the knowledge that user have to know about CoreDispatcher. I wanted to provide a quick and simple way to do execute something under UI thread without having to fiddle with where the Dispatcher is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't mean an extension method for coreDispatcher, I meant an extension method for CoreApplicationView :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we gain much from doing:
CoreApplication.MainView.Dispatcher.AwaitableRunAsync
to
CoreApplication.MainView.ExecuteOnUIThreadAsync
Even though we are ... 5 letter shorter :D
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, that's not what that method is :P you're right with what im suggesting, CoreApplication.MainView.ExecuteOnUIThreadAsync
, but I'm saying that it's instead of
DispatcherHelper.ExecuteOnUIThreadAsync(CoreApplication.Mainview, myFunc)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also think that this should be an extension method. You can still run it through DispatcherHelper if you like. But why not make it callable directly on the CoreApplicationView as well? myApplicationView.ExecuteOnUIThreadAsync(func)
sounds better to me, compared to DispatcherHelper.ExecuteOnUIThreadAsync(myApplicationView, func)
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, that is also a valid point. We can support both, there is no harm as anybody could use any options that they would like and the outcome would be the same. I'll add it in for 1.3 then (since this has already been merged?) cc @deltakosh
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, this can wait for 1.3, no need to rush it (won't be a breaking change)...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like we still have a few days to add to 1.2.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I should be able to do it tonight.
I don't understand why you're saying you have to return null. That makes sense if you expect |
Still you would need to either create an "async" lambda (although you do not execute anything async), or you would need to return a Task.FromResult(null), to satisfy the |
We can still push PR until this week end |
Addressing issue #471
Things to do: