I'm trying to upgrade an existing C# UWP UserControl I have to be used by WinUI3 C++ and WinUI3 C#.

It's awesome, I'm using a shared code project to compile it for UWP and WinUI3 everything is great except I can't seem to execute async code in my WinUI C++ test app. It just dies I can't even wrap it in a try/catch.

I decided to strip my problem down to the basics and followed along with this tutorial from Microsoft and everything is great until I change the button click event to

private async void Button_Click(object sender, RoutedEventArgs e)
{
    await Task.Delay(5000);
}

WinUI3 C++ just dies as soon as it hits the async line with

Unhandled exception at 0x75F39132 (KernelBase.dll) in CppApp.exe: 0xE0434352 (parameters: 0x8001010E, 0x00000000, 0x00000000, 0x00000000, 0x5D850000).

Anything I try like Wait() or GetAwaiter().GetResult() or GetAsyncAction() doesn't seem to work.

The WinUI3 C# app that references the same project has no issues. I feel like I'm missing something fundamental here.

How do I write async code in C# for consumption in C++?

1

There are 1 best solutions below

0
Biff MaGriff On

I couldn't figure out how to run async C# in C++ so I compiled another DLL specific to C++ that removes async.

Eg.

Task.Run(async () => await Task.Delay(5000)).Wait();