3

Mac OS X の Mono (3.2.1) でこの単純な小さなテストを実行すると、コンソールに応答が出力されず、代わりにShutting down finalizer thread timed out.
Is there something wrong with this code or is my Mono misbehaving? と表示されます。

using System;
using System.Net.Http;

namespace VendTest
{
  class MainClass
  {
        public static void Main(string[] args)
        {
            Client client = new Client();
            client.HttpClientCall();
        }
    }

    public class Client
    {
        HttpClient client;

        public Client()
        {
            client = new HttpClient();
        }

        public async void HttpClientCall()
        {
            HttpClient httpClient = new HttpClient();
            HttpResponseMessage response = await httpClient.GetAsync("http://vendhq.com");
            string responseAsString = await response.Content.ReadAsStringAsync();
            Console.WriteLine(responseAsString);
        }
    }
}
4

1 に答える 1

7

メソッドを使用することはほとんどありませんasync void。これが理由の 1 つです。実際に完了Main()する前HttpClientCall()に終了します。また、Main()終了するとアプリケーション全体が終了するため、何も出力されません。

あなたがしなければならないことは、あなたのメソッドをに変更async TaskWait()Main(). (混合するawaitWait()デッドロックが発生することがよくありますが、コンソール アプリケーションには適切なソリューションです。)

class MainClass
{
    public static void Main()
    {
        new Client().HttpClientCallAsync().Wait();
    }
}

public class Client
{
    HttpClient client = new HttpClient();

    public async Task HttpClientCallAsync()
    {
        HttpClient httpClient = new HttpClient();
        HttpResponseMessage response = await httpClient.GetAsync("http://vendhq.com");
        string responseAsString = await response.Content.ReadAsStringAsync();
        Console.WriteLine(responseAsString);
    }
}
于 2013-08-08T10:13:37.097 に答える