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;
...