How do I wait until Task is finished in C#?

后端 未结 6 887
孤街浪徒
孤街浪徒 2020-12-04 23:49

I want to send a request to a server and process the returned value:

private static string Send(int id)
{
    Task responseTask =          


        
6条回答
  •  春和景丽
    2020-12-05 00:26

    A clean example that answers the Title

    string output = "Error";
    Task task = Task.Factory.StartNew(() =>
    {
        System.Threading.Thread.Sleep(2000);
        output = "Complete";
    });
    
    task.Wait();
    Console.WriteLine(output);
    

提交回复
热议问题