1

In winform, Threre is a button and when i click for clickevent, Code is stopped on await Task.Delay(2000)

Full code is below

public Form1()
    {
        InitializeComponent();
        this.button1.Click += new EventHandler( start3 );

    }


    Func<Task<int>> ftask1 => async () =>
    {
        Console.WriteLine( "start" );
        await Task.Delay( 2000 );
        Console.WriteLine( "done" );
        return 1;
    };

    public void start3( object o , EventArgs e )
    {
        var result =  ftask1().Result;
    }`

on await Task.Delay( 2000 ); from ftask1, task is not compeleted.

What is the problem of this?

JaeWoo So
  • 568
  • 5
  • 18
  • 3
    Do not call `Result` on async `Task`. It causes deadlock. Read more about this case in [this SO question](https://stackoverflow.com/questions/15021304/an-async-await-example-that-causes-a-deadlock) and [another SO question](https://stackoverflow.com/questions/17248680/await-works-but-calling-task-result-hangs-deadlocks) – Yeldar Kurmangaliyev Jun 29 '17 at 05:23

0 Answers0