Page 1 of 1

Weird HttpClient behaviour in Jiwa

PostPosted: Fri Jul 22, 2022 11:40 am
by SBarnes
In a win forms application the following code works and doesn't freeze

Code: Select all
HttpResponseMessage response = httpClient.SendAsync(request).Result;


Inside Jiwa it freezes the application but changing the code to the following doesn't freeze

Code: Select all
HttpResponseMessage response = SendAsync(httpClient, request);

public HttpResponseMessage SendAsync(HttpClient client, HttpRequestMessage request)
{
   HttpResponseMessage response = null;
   var task = Task.Run(() => response = client.SendAsync(request).Result);
   task.Wait();
   return response;
}


It would appear Jiwa doesn't like the code executing on the main User interface thread, although I have overcome the issue what I am trying to work out is why.

Re: Weird HttpClient behaviour in Jiwa

PostPosted: Fri Jul 22, 2022 12:19 pm
by Mike.Sheen
SBarnes wrote:Inside Jiwa it freezes the application


What do you mean by inside Jiwa? In a plugin? Are you awaiting the response?

Re: Weird HttpClient behaviour in Jiwa

PostPosted: Fri Jul 22, 2022 12:20 pm
by SBarnes
Yeah in a plugin inside the client

Re: Weird HttpClient behaviour in Jiwa  Topic is solved

PostPosted: Mon Jul 25, 2022 2:46 pm
by Mike.Sheen
Works for me :P

Attached is my test plugin - it adds a tool to the inventory maintenance form, and when pressed it uses System.Net.Http.HttpClient to send a POST request and then display the response in a messagebox.

No problems with my testing!

HttpClient_Test_Response.png
HttpClient_Test_Response.png (48.38 KiB) Viewed 2045 times

Re: Weird HttpClient behaviour in Jiwa

PostPosted: Mon Jul 25, 2022 2:55 pm
by Mike.Sheen
BTW using your technique of Task.Run and then Await I found the request wasn't really asynchronous - the UI thread was blocked until execution finished - you couldn't do anything on the form whilst it was waiting the response from the web request.

Attached uses a different approach and allows the UI to respond whilst the request is being made.

You would have had your reasons for the way you did it - probably you couldn't start off with an async event handler like I could in my example.