RestSharp async background thread

Viewed 829

I'm trying to send some POST requests to a server using RestSharp. However:

  1. The server takes about 20 seconds to process each request
  2. I don't want to lock up my GUI whilst I'm waiting for them to finish.

RestSharp has async functionality, so I've written this:

var handler = _restClient.ExecuteAsync(request, response =>
                {
                    if(response.IsSuccessful)
                    {
                        Console.WriteLine("Success");
                    }
                });

What I need to do is ensure that this happens on a separate background thread, as not to lock the GUI, and to wait for each response, check if successful, and then send the next one.

Any suggestions? A queue of some kind?

1 Answers

You could consider using async/await so as not to lock up the UI thread

var response = await _restClient.ExecuteTaskAsync(request);
if(response.IsSuccessful) {
    Console.WriteLine("Success");
}
Related