0

So I am working on previously written code that I want to check for performance improvements and i came across lines like these littered here and there. To me these look like not achieving anything extra. Maybe I am missing something.

somewebclient.PostAsync(someurl, somecontent).Result()

or

Dim req = mpfcontent.ReadAsStringAsync()
                    req.Wait()
                    Dim reqContent = req.Result

Do these really add any value in terms of performance once i do a Result or Wait on them ? Again I am looking to see if I can increase asynchronous behavior of the web service but not without knowing what i am doing? I need some insights into these functions.

Som Bhattacharyya
  • 3,972
  • 35
  • 54
  • 1
    Using `Result` (without knowing that the `Task` is Completed) or `Wait` is a great way to potentially create deadlocks. So whatever changes you're planning to make to these, I'd say do it. – Damien_The_Unbeliever Nov 28 '18 at 06:52
  • Can you advise me how to keep the call actually asynchronous and handle the result in a call back sort of fashion . I need some pointers. – Som Bhattacharyya Nov 28 '18 at 07:03
  • 1
    In modern code, you use `Await` on `Task`s. What comes after the `Await` is effectively like a callback but all of the heavy lifting is done by the compiler. – Damien_The_Unbeliever Nov 28 '18 at 07:04
  • okk. Thanks man. I will do some reading on that. Our web service is having performance issues and I am entrusted with figuring out and fixing all this mess :) – Som Bhattacharyya Nov 28 '18 at 07:06
  • 1
    Be aware that once you start using async, you tend to(or are required to) implement it all the way(up/down) https://stackoverflow.com/questions/29808915/why-use-async-await-all-the-way-down. I don't necessarily agree with @Damien_The_Unbeliever that calling Result() or Wait() induces deadlocks, it depends on what the remainder of the code does. Try benchmarking the application before assuming any performance benefits from async code, the issue could be someplace else. – Alexander Pope Nov 28 '18 at 09:53
  • I understand you mean depending the context its called on. Its used in a ASMX webmethod context as well as elsewhere in a console application. Wjat do you recommend. Do i need to first migrate the whole thing to WCF and then think of anything ? – Som Bhattacharyya Nov 28 '18 at 09:55
  • 1
    It really depends on your functional and time requirements. I'd benchmark the app using performance profilers, analyze the code quality/ease of maintenance and then make an informed decision. It is not a question we can answer on SO. – Alexander Pope Nov 28 '18 at 10:06
  • I understand but i want to discuss this with someone with more experience than me. :) – Som Bhattacharyya Nov 28 '18 at 10:09

0 Answers0