1

The Windows Forms GUI thread keeps stalling for a second now and then during async HttpClient SendAsync. Except for that, everything works fine (I get data). I want to do frequent and hopefully parallel requests to different servers to update the screen frequently. I am trying to launch multiple requests that post the responses for processing by the GUI thread later. The code below is a simplification of my code. I've checked the time before and after SendAsync and see it is sometimes up to 2 seconds while the GUI window is frozen (can't be moved, scrolled, etc) and the polling timer is inactive (counter never incremented).

Using async Task DoWork did not help.

class Worker
{
    HttpClient client = new HttpClient();
    bool busy = false;
    string data = "";
    //public async Task DoWork()
    public async void DoWork()
    {
        if ( busy ) return;
        busy = true;
        HttpRequestMessage request = new HttpRequestMessage(method, requestUrl);
        HttpResponseMessage response = await client.SendAsync( request );
        data = ... from response ...
        busy = false;
    }
}
int counter;
private void Update(object sender, EventArgs e)
{
    ++counter;
    foreach ( Worker worker in workers )
        worker.DoWork();
}
...
List<Worker> workers = ...
var poll = new System.Windows.Forms.Timer();
poll.Tick += Update;
poll.Interval = 250;
poll.Enabled = true;
...
Bogdan Doicin
  • 2,342
  • 5
  • 25
  • 34
Codemeister
  • 107
  • 1
  • 6
  • Try sharing the client (declare it static), HttpClient is a heavy object and the proper way to use it is to reuse it. – arynaq May 05 '19 at 12:49
  • Can 1 HttpClient be used for multiple simultaneous requests to multiple servers? I was using 1 per server. – Codemeister May 05 '19 at 13:56
  • https://stackoverflow.com/questions/24983635/httpclient-sendasync-using-the-thread-pool-instead-of-async-io mentions that SendAsync is not completely async, and uses a threadpool internally; Could the threadpoll be too small? – Codemeister May 05 '19 at 13:58
  • How have you determined that `GetAsync` is the issue? – John Wu May 05 '19 at 14:42
  • Issue is not with SendAsync but with GUI updating being very slow – Codemeister May 06 '19 at 20:07

2 Answers2

0

If you are using Windows.Forms you may consider BackgroundWorker

Ivan Ičin
  • 9,672
  • 5
  • 36
  • 57
  • Thanks. BackgroundWorker allowed separating the HttpClient work from the UI - and it became clear the UI was the bottleneck. My measurement method was faulty. – Codemeister May 05 '19 at 15:35
0

SendAsync is sufficiently asynchronous (even if it uses threads internally). Updating UI itself is really slow and the cause of UI freezing.

oguz ismail
  • 1
  • 16
  • 47
  • 69
Codemeister
  • 107
  • 1
  • 6
  • 1
    This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - [From Review](/review/low-quality-posts/22986661) – VRAwesome May 11 '19 at 18:35
  • I was trying to respond to my own question; The problem was not with SendAsync, but with slow updates to DataGridView. By putting them on different threads (BackgroundWorker) made that clear. – Codemeister May 12 '19 at 22:25