-1

I need to call an async method from MeasureOverride. This call should be synchronous because I need to return the value of the async method.

I tried about a dozen different calls from these threads: Synchronously waiting for an async operation, and why does Wait() freeze the program here How would I run an async Task<T> method synchronously?

They all failed, some wouldn't compile in Universal Windows Applications, some weren't synchronous, some provoked a deadlock etc..

I feel strongly against async/await because of how it contaminates the call hierarchy prototypes. At the override level I'm stuck with no async keyword and I need to call await synchronously from the blackbox. Concretely how should I go about it?

Community
  • 1
  • 1
user1169933
  • 11
  • 1
  • 5

1 Answers1

1

The first thing you should do is take a step back. There should be no need to call asynchronous code from within a MeasureOverride in the first place.

Asynchronous code generally implies I/O-bound operations. And a XAML UI element that needs to send a request to a remote web server, query a database, or read a file, just to know what its size is, is doing it wrong. That's a fast track to app certification rejection.

The best solution is almost certainly to move the asynchronous code out of the element itself. Only create/modify the UI elements after the necessary asynchronous work completes.

That said, if you feel you must block the UI thread while asynchronous operations complete, I've compiled a list of every hack I know of, published in my MSDN article on brownfield asynchronous development.

Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810
  • So basically you're making a bunch of false supposition of how my application is set-up and what it's doing. Then you suggest I re-architecture thousands of lines of codes that are perfectly working in WPF? Instead of making a quick patch that has all the technical reasons to be able to work once I figure out the correct syntax? – user1169933 Dec 28 '15 at 05:17
  • Not sure by your response if you have ever done WPF programming but here is the basic workflow, MeasureOverride is called by WPF, I need to report the size now and then. The size comes from an await method, no deal not negotiable, that's the black box. – user1169933 Dec 28 '15 at 05:20
  • Basically what you suggest is recreating WPF GUI framework. WPF creates the element not you, hence you have no control on await-async within the GUI framework. – user1169933 Dec 28 '15 at 05:22
  • 2
    @user1169933 - you appear to think the community has let you down. Not sure why. Questions on StackOverflow are answered by volunteers. There is no SLA and you are not entitled to an immediate response. Plus you posted your question in the middle of the Christmas season. So, please, chill out. Insulting people who are trying to help you is no way to carry on. – APC Dec 28 '15 at 08:42
  • 2
    @user1169933: `The size comes from an await method, no deal not negotiable, that's the black box.` Then I recommend you first call the async method and then create the UI element with a known size. – Stephen Cleary Dec 28 '15 at 15:06