我有一个关于向服务器执行大量异步不成功请求的进程的奇怪行为的问题。奇怪的是,退出/停止/终止进程几乎是不可能的。如果您在 Windows 8 机器上编译并运行该程序,则该进程会挂起,我还没有找到如何杀死它的解决方案。只有重新启动有帮助。
有人可以解释一下上述行为吗?
谢谢。
PS:这是我使用taskmanager杀死进程时得到的
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;
}
}
}