URLDownloadToFile API, how can it be used asynchronously? - c++

How can this API URLDownloadToFile be used asynchronously? I need to show the progress of the download via SendMessage to a client window, which can't be done as the API appears to be synchronous and it never sends the OnProgress until the download completes.
I have also seen some example codes involving IMoniker interface, but I can't find an example that involves asynchronous reading of data and saving them to a file.
Thanks in advance.

Use URLOpenPullStream instead.

Run it in a new thread using CreateThread(). Then your main thread can process the message queue as normal.

Related

How to properly use the asynchronous libusb?

I worked on the synchronous libusb in my Qt project with good results and now I need the asynchronous features of this library. I understood reading here, here and here that, after I've registered my callback function using the libusb_fill_control_transfer and submitted a transfer with libusb_submit_transfer , I need to "keep live" the libusb_handle_events_completed inside a while loop to get the transfer related events since the libusb doesn't have its own thread. for example you can read a code like this
libusb_fill_control_transfer(transfer, dev, buffer, cb, &completed, 1000);
libusb_submit_transfer(transfer);
while (!completed) {
libusb_handle_events_completed(ctx, &completed);
}
Now if I want read a packet that I don't know when it occurs, I think that goes against the asynchronous nature submit a read and wait in the while with libusb_handle_events_completed until the event is triggered.
Then, do I need to create a separate thread within the libusb_handle_events_completed in an infinite while loop?
Can anyone, with experience in the asynchronous features of libusb library, give some suggestions on the right approach to handle the transfer events?

postgresql query c++ callback

Is it possible with any existing c++ library to implement a callback that returns when a query is completed?
I've found this, but I'm not sure if that's what I want.
I'd like to wait the boost::thread writing to the database until the write is completed.
If this is possible, please link the library and an example.
I was looking for something like this too, ended up doing an async libpq wrapper: http://github.com/metherealone/postgrespp - It uses Boost.ASIO though, not threads. I hope this helps.
From the libpq docs about PQexec function:
Submits a command to the server and waits for the result.
Similar to that, the PQexecParams and PQexecPrepared also wait for the query to be executed. So, using those functions you don't need to worry about waiting, the API will do that for you.
If you need to asynchronously send query to the database, you can use the asynchronous functions.

C++/C Best way to detect when a folder contents changed and send the files that do via HTTP

What's the best way to scan a folder and be notified when a new file has been created. I'm using Windows XP and I need a solution in C++ or C.
I need to send those files via HTTP to the server.
Wondering what would be the best solution for this? open to using some third party library.
Thank you for your help.
Use FindFirstChangeNotification function with FILE_NOTIFY_CHANGE_FILE_NAME as last argument.
It will return you a handler which then you need to observe with WaitForSingleObject.
This second function will wait for a specified amound of time or for a notification from a handler.
If you would like to break it manualy, use MsgWaitForMultipleObjects instead and provide two handles: first handle is the one you get from FindFirstChangeNotification, and the second can be a handler from your own event (use CreateEvent to create it) which you can trigger with SetEvent(handle_you_get_from_CreateEvent)

Boost ASIO Network Timing Issue

I am using boost::asio to implement network programming and running into timing issues. The issue is currently most with the client.
The protocol initially begins by the server returning a date time string to the user, and the client reads it. Up to that part it works fine. But What I also want is to be able to write commands to the server which then processes them. To accomplish this I use the io_service.post() function as shown below.
io_service.post(boost::bind()); // bounded function calls async_write() method.
For some reason the write tries happens before the initial client/server communication, when the socket has not been created yet. And I get bad socket descriptor error.
Now the io_service's run method is indeed called in another thread.
When I place a sleep(2) command before post method, it work fine.
Is there way to synchronize this, so that the socket is created before any posted calls are executed.
When creating the socket and establishing the connection using boost::asio, you can define a method to be called when these operations have either completed or failed. So, you should trigger your "posted call" in the success callback.
Relevant methods and classes are :
boost::asio::ip::tcp::resolver::async_resolve(...)
boost::asio::ip::tcp::socket::async_connect(...)
I think the links below
will give u some help
http://www.boost.org/doc/libs/1_42_0/doc/html/boost_asio/reference/io_service.html

How to handle asynchronous socket receiving in C++?

I'm currently using a thread to handle Connect and Send calls asynchronously. This is all working fine, but now I want to make receiving asynchronous too. How should I receive data without pausing the whole queue while waiting for data? The only solution I can think of right now is a second thread.
Look into non-blocking sockets and polling APIs like select(2)/poll(2)/epoll(4)/kqueue(2).
Specifically in C++, look into boost::asio.
Depending on what you're doing, non-blocking I/O with select may be the answer.
Take a look at this tutorial.