2017-10-11 9 views
0

私はasync awaitをC#で頭の中に入れようとしています。私は2つのファイルを持つこの小さなWindowsコンソールアプリケーションを書いています。C#のmainからasync関数を呼び出す

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(); 

     } 
    } 
} 

私はちょうど私のmainから機能DownloadFilesAsync()を呼びたいです。私はDownloaderオブジェクト 'd'を作成しました。しかし、それは主で戻り型は空でなければならないので不可能である。これを回避する方法は何ですか?

+0

https://www.bing.com/search?q=calling+an+async+function+from+main+in+c%23 –

答えて

1
Task.Run(async() => { await d.DownloadFilesAsync();}).Wait(); 
関連する問題