2

I have a question regarding strange behavior of a process that performs lots of async unsuccessful requests to a server. The strange thing is that it's almost impossible to exit/stop/kill the process. If you compile and run this program on a windows 8 machine the process hangs and I haven't found a solution how to kill it. Only reboot helps.

Could somebody explain the mentioned behavior please.

Thanks.

PS: This is what I get when use taskmanager to kill the process

This is what I get when use taskmanager to kill the process

internal class Program
{
    private const int totalRequests = 2000;

    private static void Main(string[] args)
    {
        var cancellationTockenSource = new CancellationTokenSource();
        LoadTest(cancellationTockenSource);

        Console.WriteLine("Press any key to break");
        Console.ReadLine();
        cancellationTockenSource.Cancel();
    }

    private static async Task LoadTest(CancellationTokenSource cancellationTockenSource)
    {
        Stopwatch stopWatch = Stopwatch.StartNew();
        var requests = new List<Task>(totalRequests);

        for (int i = 0; i < totalRequests; ++i)
        {
            requests.Add(Request(cancellationTockenSource.Token));
        }
        try
        {
            await Task.WhenAll(requests);
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }

        stopWatch.Stop();
        Console.WriteLine("{0} req/sec", stopWatch.Elapsed.TotalSeconds/totalRequests);
    }

    private static HttpRequestMessage CreateMessage()
    {
        var url = new Uri("http://ya.ru:8080/234234234234x");
        var message = new HttpRequestMessage(HttpMethod.Get, url);
        message.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/json"));
        return message;
    }

    protected static async Task<string> Request(CancellationToken token)
    {
        using (var client = new HttpClient())
        {
            HttpResponseMessage response = await client.SendAsync(CreateMessage(), token);
            string content = await response.Content.ReadAsStringAsync();
            return content;
        }
    }
}
4

0 に答える 0