My wpf program reads commands from a text file, and then sends those commands out over HttpClient. Each GetAsync response takes about 2 seconds to complete. This seems excessively long, especially when I have 50 commands to send.
Is this a normal amount of time for HttpClient to send/receive a GetAsync Message? Is there a faster way to do this?
static readonly HttpClient client = new HttpClient();
public MainWindow()
{
InitializeComponent();
}
private async void Register_ClickAsync(object sender, RoutedEventArgs e)
{
int counter = 0;
string line;
System.IO.StreamReader file = new System.IO.StreamReader(@"C\path.txt");
while ((line = file.ReadLine()) != null)
{
try
{
var watch = Stopwatch.StartNew();
HttpResponseMessage response = await client.GetAsync(line);
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
watch.Stop();
var elapsedMS = watch.ElapsedMilliseconds;
RequestTextBox.Text += "\n" + elapsedMS;
}
catch (HttpRequestException ex)
{
Console.WriteLine("\nException Caught!");
Console.WriteLine("Message :{0} ", ex.Message);
this.RequestTextBox.Text += ("\n" + "Message: {0} ", ex.Message);
}
}
file.Close();
}
}
UPDATE: This original program is a .net Core WPF app. I created a .net framework WPF app with the exact same code. The .net framework app takes 2000 ms to send the first HttpClient GetAsync command, and then <10 ms for subsequent commands. This is a massive performance increase. Does anyone have an explanation? I have been unable to find any articles explaining this.
Your code is awaiting for each request.
You can create many tasks by calling GetAsync and then wait them all with Task.WhenAll(). It will be more faster.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With