2017-10-11 1 views
0

Ich versuche, meinen Kopf um async await in C# zu wickeln. Ich habe diese kleine Windows-Konsolen-App geschrieben, die zwei Dateien enthält.Aufruf einer asynchronen Funktion von Haupt in C#

Downloader.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading; 
using System.Threading.Tasks; 

namespace AsyncAwait 
{ 
    public class Downloader 
    { 

     public async Task DownloadFilesAsync() 
     { 
      // In the Real World, we would actually do something... 
      // For this example, we're just going to print file 0, file 1. 
      await DownloadFile0(); 
      await DownloadFile1(); 
     } 
     public async Task DownloadFile0() 
     { 
      Console.WriteLine("Downloading File 0"); 
      await Task.Delay(100); 
     } 

     public async Task DownloadFile1() 
     { 
      Console.WriteLine("Downloading File 1"); 
      await Task.Delay(100); 
     } 
    } 
} 

Program.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace AsyncAwait 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      Downloader d = new Downloader(); 

     } 
    } 
} 

Ich möchte nur die Funktion DownloadFilesAsync() von meinem main nennen. Ich habe das Downloader Objekt 'd' erstellt. Da es sich jedoch um Haupt- und Rückgabetyp handelt, muss dieser ungültig sein. Dies ist nicht möglich. Was ist ein Weg darum?

+0

https://www.bing.com/search?q=calling+eine+async+funktion+von+main+in+c%23 –

Antwort

1
Task.Run(async() => { await d.DownloadFilesAsync();}).Wait(); 
Verwandte Themen