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