title: How do we await for C# async delegate function in C++/CX
body: <p>This is about the communication between the <code>C++</code> (shared code of different platforms) with <code>C#</code> (Windows Universal App). We know that the following is how we make a function call from <code>C++</code> to <code>C#</code>. </p>
  
  <p><strong>C#</strong> </p>
  
  <pre><code>class FooCS
  {
      FooCS()
      {
          FooC c = new ref FooC(); 
          c.m_GetSomeValueEvent = GetSomeValueEvent;
          // Some other stuff ...
      }
  
      string GetSomeValueEvent()
      {
          // Some other stuff ... 
          return "Hello World"; 
      }
  }
  </code></pre>
  
  <p><strong>C++</strong></p>
  
  <pre><code>public delegate Platform::String GetSomeValueEventHandler(); 
  
  class FooC
  {
      public:
      event GetSomeValueEventHandler^ m_GetSomeValueEvent; 
      void someFunction(){
          Platform::String^ str = m_GetSomeValueEvent();
          // Some other stuff ... 
      }
  }
  </code></pre>
  
  <p>The above is very straight forward. But the problem is that this string <code>GetSomeValueEvent()</code> in <code>C#</code> do some heavy task such as reading data from the database, and I have to make it <code>async</code>. </p>
  
  <pre><code>    async Task&lt;string&gt; GetSomeValueEvent() {
          // Some other stuff ... 
          return "Hello World"; 
      }
  </code></pre>
  
  <p>Now here comes the question: How do I make my <code>C++</code> code wait for the return of this delegate function? In another word, what should be the following signature be modified to?</p>
  
  <pre><code>    public delegate Platform::String GetSomeValueEventHandler(); 
  </code></pre>
  
  <p>I have been googling around and cannot find the right terminology for this problem. If you know for sure that it is impossible, would be at least nice to know. </p>
  
  <hr>
  
  <p><strong>Eventually</strong>, this is what we do: </p>
  
  <pre><code>    string GetSomeValueEvent() {
          // Convert a async method to non-async one
          var val = someAsyncMethod().Result; 
          // Some other stuff ... 
          return "Hello World"; 
      }
  </code></pre>
  
  <p>You have to make sure <code>GetSomeValueEvent</code> is not running on the main thread to prevent deadlock. </p>