Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Question About Async/Await

Question About Async/Await

Scheduled Pinned Locked Moved C#
questionregexhelptutorialdiscussion
9 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kevin Marois
    wrote on last edited by
    #1

    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.

    A D L 3 Replies Last reply
    0
    • K Kevin Marois

      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.

      A Offline
      A Offline
      Alex Schunk
      wrote on last edited by
      #2

      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.

      K 1 Reply Last reply
      0
      • A Alex Schunk

        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.

        K Offline
        K Offline
        Kevin Marois
        wrote on last edited by
        #3

        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.

        A 1 Reply Last reply
        0
        • K Kevin Marois

          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.

          A Offline
          A Offline
          Alex Schunk
          wrote on last edited by
          #4

          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.

          K 1 Reply Last reply
          0
          • K Kevin Marois

            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.

            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #5

            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

            K 1 Reply Last reply
            0
            • D 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 Kreskowiak

              K Offline
              K Offline
              Kevin Marois
              wrote on last edited by
              #6

              Yes

              If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

              1 Reply Last reply
              0
              • A Alex Schunk

                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.

                K Offline
                K Offline
                Kevin Marois
                wrote on last edited by
                #7

                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.

                A 1 Reply Last reply
                0
                • K Kevin Marois

                  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.

                  A Offline
                  A Offline
                  Alex Schunk
                  wrote on last edited by
                  #8

                  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.

                  1 Reply Last reply
                  0
                  • K Kevin Marois

                    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.

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    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

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups