Question About Async/Await
-
I've read that it's not a good idea to fire an sync method from a property, yet I see plenty of places where I would want to do this. For example, at the top of my Contacts view I have a Search field bound to a ContactSearchValue string property. When the user enters a seaarch value I reload all the contacts that match. To do this I call LoadContacts from the settter of the property. Because LoadContacts is async I get a compilation warning saying "Because this call is not awaited, execution of the current method continues before the call is completed" This is OK beause it in a property settings and there's no other code. Anyone have any thoughts on this?
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
-
I've read that it's not a good idea to fire an sync method from a property, yet I see plenty of places where I would want to do this. For example, at the top of my Contacts view I have a Search field bound to a ContactSearchValue string property. When the user enters a seaarch value I reload all the contacts that match. To do this I call LoadContacts from the settter of the property. Because LoadContacts is async I get a compilation warning saying "Because this call is not awaited, execution of the current method continues before the call is completed" This is OK beause it in a property settings and there's no other code. Anyone have any thoughts on this?
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
It is not a good idea because it is fire-and-forget. Unit-testing this kind of code is a pain or plainly not possible. But yes you can do it... Just be aware of the bad stuff that may happen.
-
It is not a good idea because it is fire-and-forget. Unit-testing this kind of code is a pain or plainly not possible. But yes you can do it... Just be aware of the bad stuff that may happen.
How else would you reload the contacts when a search value is entered?
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
-
How else would you reload the contacts when a search value is entered?
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
It is mostly done in the SearchCommand... Which is triggered if the user hits the enter key or if a small amount of grace period has passed.
-
I've read that it's not a good idea to fire an sync method from a property, yet I see plenty of places where I would want to do this. For example, at the top of my Contacts view I have a Search field bound to a ContactSearchValue string property. When the user enters a seaarch value I reload all the contacts that match. To do this I call LoadContacts from the settter of the property. Because LoadContacts is async I get a compilation warning saying "Because this call is not awaited, execution of the current method continues before the call is completed" This is OK beause it in a property settings and there's no other code. Anyone have any thoughts on this?
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
Kevin Marois wrote:
I've read that it's not a good idea to fire an sync method from a property
Did you mean "async method", not "sync"?
Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
Dave Kreskowiak -
Kevin Marois wrote:
I've read that it's not a good idea to fire an sync method from a property
Did you mean "async method", not "sync"?
Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
Dave KreskowiakYes
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
-
It is mostly done in the SearchCommand... Which is triggered if the user hits the enter key or if a small amount of grace period has passed.
That seems like a lot of work - hook up a command to a property to fire a method??
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
-
That seems like a lot of work - hook up a command to a property to fire a method??
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
It just sound like a lot of work, but it isnt. If you have fire and forget async methods in the property, you can't control what they are doing. You don't know if there are started or finished or whatever... If you have somewhere code that depends on the result of your fire and forget code, then you will looking for bugs that you will never find. If your fire and forget code is just updating visual stuff (like icons or colors...) then well go for it... If other logic depends on it... Don't do it. Well actually you can do it, if you a fan of bugs. Debugging is a fun hobby. More fun if you are debugging asyncronous code. On top of that... Fire and forget code is very hard (most of the time impossible) to unit test.
-
I've read that it's not a good idea to fire an sync method from a property, yet I see plenty of places where I would want to do this. For example, at the top of my Contacts view I have a Search field bound to a ContactSearchValue string property. When the user enters a seaarch value I reload all the contacts that match. To do this I call LoadContacts from the settter of the property. Because LoadContacts is async I get a compilation warning saying "Because this call is not awaited, execution of the current method continues before the call is completed" This is OK beause it in a property settings and there's no other code. Anyone have any thoughts on this?
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
Any searching I do, usually incrementally on a separate thread, gets invoked from the "text changed" event of the target textbox. Keyed input get queued or discarded depending on what the "search" is doing at any given time. The search might involve a web service (e.g. postal addresses).
"(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal