Weird HttpClient behaviour in Jiwa  Topic is solved

Discussions relating to plugin development, and the Jiwa API.

Weird HttpClient behaviour in Jiwa

Postby SBarnes » Fri Jul 22, 2022 11:40 am

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.
Regards
Stuart Barnes
SBarnes
Shihan
Shihan
 
Posts: 1619
Joined: Fri Aug 15, 2008 3:27 pm
Topics Solved: 175

Re: Weird HttpClient behaviour in Jiwa

Postby Mike.Sheen » Fri Jul 22, 2022 12:19 pm

SBarnes wrote:Inside Jiwa it freezes the application


What do you mean by inside Jiwa? In a plugin? Are you awaiting the response?
Mike Sheen
Chief Software Engineer
Jiwa Financials

If I do answer your question to your satisfaction, please mark it as the post solving the topic so others with the same issue can readily identify the solution
User avatar
Mike.Sheen
Overflow Error
Overflow Error
 
Posts: 2444
Joined: Tue Feb 12, 2008 11:12 am
Location: Perth, Republic of Western Australia
Topics Solved: 756

Re: Weird HttpClient behaviour in Jiwa

Postby SBarnes » Fri Jul 22, 2022 12:20 pm

Yeah in a plugin inside the client
Regards
Stuart Barnes
SBarnes
Shihan
Shihan
 
Posts: 1619
Joined: Fri Aug 15, 2008 3:27 pm
Topics Solved: 175

Re: Weird HttpClient behaviour in Jiwa  Topic is solved

Postby Mike.Sheen » Mon Jul 25, 2022 2:46 pm

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 494 times
Attachments
Plugin HTTPClientTest.xml
(21.08 KiB) Downloaded 49 times
Mike Sheen
Chief Software Engineer
Jiwa Financials

If I do answer your question to your satisfaction, please mark it as the post solving the topic so others with the same issue can readily identify the solution
User avatar
Mike.Sheen
Overflow Error
Overflow Error
 
Posts: 2444
Joined: Tue Feb 12, 2008 11:12 am
Location: Perth, Republic of Western Australia
Topics Solved: 756

Re: Weird HttpClient behaviour in Jiwa

Postby Mike.Sheen » Mon Jul 25, 2022 2:55 pm

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.
Attachments
Plugin Async HTTPClientTest.xml
(20.86 KiB) Downloaded 46 times
Mike Sheen
Chief Software Engineer
Jiwa Financials

If I do answer your question to your satisfaction, please mark it as the post solving the topic so others with the same issue can readily identify the solution
User avatar
Mike.Sheen
Overflow Error
Overflow Error
 
Posts: 2444
Joined: Tue Feb 12, 2008 11:12 am
Location: Perth, Republic of Western Australia
Topics Solved: 756


Return to Technical and or Programming

Who is online

Users browsing this forum: Google [Bot] and 35 guests