Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ Library : Other (not classified elsewhere) » Custom abort of HttpClient::Execute()
icon5.gif  Custom abort of HttpClient::Execute() [message #6692] Thu, 23 November 2006 11:00 Go to next message
mezise is currently offline  mezise
Messages: 54
Registered: April 2006
Member
How to force a cancellation of httpClient.Execute() before httpClient.TimeoutMsecs(msec), e.g. on Button action?

I have dug through the code with no success.

Michal

[Updated on: Thu, 23 November 2006 11:04]

Report message to a moderator

Re: Custom abort of HttpClient::Execute() [message #6697 is a reply to message #6692] Thu, 23 November 2006 16:11 Go to previous messageGo to next message
rylek is currently offline  rylek
Messages: 79
Registered: November 2005
Member
You can abort the HTTP request using the progress callback

Gate2<int, int> progress

sent as one of the parameters to Execute. This callback is called periodically during all phases of the request (connection to server, writing request data, reading server response); as the name of the identifier implies, it's typically used to display progress bar.

In contrast to Callback2<int, int>, Gate2 has a return value. When you implement the progress callback, by returning true from the Gate function you tell the HttpClient to abort the request immediately.

A typical code snippet demonstrating its use within GUI context:

class DlgLoadURL : WithLoadURLLayout<TopWindow> {
public:
typedef DlgLoadURL CLASSNAME;
DlgLoadURL();

private:
void Cancel();
bool Progress(int pos, int size);

private:
bool canceled;
};

DlgLoadURL::DlgLoadURL()
{
CtrlLayoutOKCancel(...);
cancel <<= THISBACK(Cancel);
}

DlgLoadURL::Cancel()
{
canceled = true;
}

void DlgLoadURL::ExecuteHTTPRequest()
{
canceled = false;
HttpClient client;
// fill in input request information
client.Execute(THISBACK(Progress));
// process result or error
}

bool DlgLoadURL::Progress(int pos, int size)
{
// you can use pos & size to set up progress meter
Ctrl::ProcessEvents();
return canceled;
}

Note the call to Ctrl::ProcessEvents(); this is necessary to run the message loop during HTTP request execution (the outer message loop naturally doesn't run because from the point of view of the surrounding code the program is stuck within the HttpClient::Execute method). Without the message loop it wouldn't be possible to register user clicking on the Cancel button.

When the user clicks the Cancel button, the member variable 'canceled' is set to 'true'. The next time Progress is called, true is returned and the HTTP client terminates the request.

Practical remark: current version of the HTTP client calls the progress callback every few hudred milliseconds or so; when you use this method to cancel the pending request, expect an appropriate time lag. Getting rid of the lag would require either a multithreaded approach or marrying the socket interface with the Windows / Linux event loop, which is a direction we are still very reluctant to pursue.

Regards

Tomas
icon14.gif  Re: Custom abort of HttpClient::Execute() [message #6708 is a reply to message #6697] Thu, 23 November 2006 22:49 Go to previous message
mezise is currently offline  mezise
Messages: 54
Registered: April 2006
Member
Thank you for very exhaustive reply.
Applying it to a multithreaded code works like a charm!

Regards

Michal
Previous Topic: Help:Thirdparty function call crashed
Next Topic: background color of the cell in the GridCtrl
Goto Forum:
  


Current Time: Sat Apr 27 03:53:26 CEST 2024

Total time taken to generate the page: 0.05609 seconds