Я попытался воспроизвести ситуацию с тупиком, как в этом примере:
https://blog.stephencleary.com/2012/07/dont-block-on-async-code.html
вот код, который я запускаю в консольном приложении:
class Program
{
static async Task<int> f()
{
var t = Task.Delay(1000); //and not Task.Delay(1000).ConfigureAwait(false);
await t;
return 1; // line execute in a woker thread
}
static async Task g()
{
int p=await f();
}
static void Main(string[] args)
{
var t=g(); //0
t.Wait();
}
}
этот код не блокируется: return 1 является выдержкой в другом потоке, хотя я не написал Task.Delay (1000) .ConfigureAwait (false);
Но это приложение wpfу меня тупик, как я и предвидел:
<Application x:Class="WpfApplication3.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication3"
Startup="Application_Startup">
<Application.Resources>
</Application.Resources>
public partial class App : Application
{
public async Task<int> f()
{
var t = Task.Delay(1000)
await t;
return 1;
}
public async Task g()
{
int p = await f();
a = 1 + p;
}
private void Application_Startup(object sender, StartupEventArgs e)
{
var t = g();
t.Wait();
}
}
Я не понимаю, почему консольное приложение работает без взаимоблокировок, а у wpf-приложений тупик.
Спасибо за помощь